0.     前期准备
官方protobuf定义

https://code.google.com/p/protobuf/

 
python使用指南
https://developers.google.com/protocol-buffers/docs/pythontutorial
http://blog.csdn.net/love_newzai/article/details/6906459
 
 
安装 python对protobuf的支持
 
wget https://protobuf.googlecode.com/files/protobuf-2.5.0.tar.bz2
tar -vxjf protobuf-2.5.0.tar.bz2
cd protobuf-2.5.0
./configure --prefix=/home/admin/mypython/
make ; make install
 
 
1     准备.proto文件
 
struct_oss_pb.proto
 message entity_attr
{
required int32 attr_id = 1; // 属性类型标识,比如:标题属性为 1,正文属性为2,图片属性为 3,发现时间属性为4,原始url属性为5 ,父页面属性为 6;
required bytes attribute = 2; // 属性类型描述,比如“标题”,“ 正文”,“图片”,“发现时间”,“原始 url”,“父页面 ”等
repeated bytes value = 3; // 属性值,除“图片”只保留 osskey之外,其他保留原文。考虑到文章中会保留多幅图,所以采用repeated。
}; message entity_desc
{
required int32 entity_id = 1; // 实体类型标识,比如:新闻为 1,小说为2 。
required bytes entity_name = 2; // 实体名称,比如:新闻主题事件关键词,小说名等。
repeated entity_attr attributes = 3; // 属性描述,格式见entity_attr。
};
2.     将proto转化为 xxx_pb2.py ,然后在你的程序里import这个py
 
protoc --python_out=./ ./struct_oss_pb.proto
 
得到struct_oss_pb_pb2.py
3.     读写protobuf的示例python
test_pb.py
01 # coding: gbk
02 import struct_oss_pb_pb2
03 entitydesc=struct_oss_pb_pb2.entity_desc()
04 entitydesc.entity_id=1
05 entitydesc.entity_name='haha'
06
07 #create proto
08 entityattr=entitydesc.attributes.add() #嵌套message
09 entityattr.attr_id = 11
10 entityattr.attribute = '标题'.decode('gbk').encode('utf-8')
11 entityattr.value.append("title adfadf")
12
13 entity_attr_str=entityattr.SerializeToString()
14 print entity_attr_str
15 entitydesc_str=entitydesc.SerializeToString()
16 print entitydesc_str
17 print '----'
18 #read
19 entityattr2 = struct_oss_pb_pb2.entity_attr()
20 entityattr2.ParseFromString(entity_attr_str)
21 print entityattr2.attr_id
22 print entityattr2.attribute.decode('utf-8').encode('gbk')
23 for i in entityattr2.value:
24 print i
25
26 print '----'
27 entitydesc2=struct_oss_pb_pb2.entity_desc()
28 entitydesc2.ParseFromString(entitydesc_str)
29 print entitydesc2.entity_id
30 #repeated entity_attr attributes,由于是repeated需要遍历
31 for oneatt in entitydesc2.attributes:
32 print oneatt.attr_id
33 for i in oneatt.value:
34 print i

Protobuf定义了一套基本数据类型。几乎都可以映射到C++\Java等语言的基础数据类型.

protobuf 数据类型

描述

打包

C++语言映射

bool

布尔类型

1字节

bool

double

64位浮点数

N

double

float

32为浮点数

N

float

int32

32位整数、

N

int

uint32

无符号32位整数

N

unsigned int

int64

64位整数

N

__int64

uint64

64为无符号整

N

unsigned __int64

sint32

32位整数,处理负数效率更高

N

int32

sing64

64位整数 处理负数效率更高

N

__int64

fixed32

32位无符号整数

4

unsigned int32

fixed64

64位无符号整数

8

unsigned __int64

sfixed32

32位整数、能以更高的效率处理负数

4

unsigned int32

sfixed64

64为整数

8

unsigned __int64

string

只能处理 ASCII字符

N

std::string

bytes

用于处理多字节的语言字符、如中文

N

std::string

enum

可以包含一个用户自定义的枚举类型uint32

N(uint32)

enum

message

可以包含一个用户自定义的消息类型

N

object of class

 

python读写protobuf的更多相关文章

  1. Python读写文件

    Python读写文件1.open使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. file_object = open('t ...

  2. python 读写、创建 文件

    python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 返回指定目录下的所有文件和目 ...

  3. [转]用Python读写Excel文件

    [转]用Python读写Excel文件   转自:http://www.gocalf.com/blog/python-read-write-excel.html#xlrd-xlwt 虽然天天跟数据打交 ...

  4. [Python]读写文件方法

    http://www.cnblogs.com/lovebread/archive/2009/12/24/1631108.html [Python]读写文件方法 http://www.cnblogs.c ...

  5. python读写Excel文件的函数--使用xlrd/xlwt

    python中读取Excel的模块或者说工具有很多,如以下几种: Packages 文档下载 说明 openpyxl Download | Documentation | Bitbucket  The ...

  6. 使用Python读写csv文件的三种方法

    Python读写csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 前言 逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是 ...

  7. python读写word、excel、csv、json文件

    http://blog.csdn.net/pipisorry/article/details/50368044 python读写word文档 (include wps)将word文档转换成txt文档 ...

  8. python读写csv文件

    文章链接:https://www.cnblogs.com/cloud-ken/p/8432999.html Python读写csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 前言 逗 ...

  9. python 全栈开发,Day86(上传文件,上传头像,CBV,python读写Excel,虚拟环境virtualenv)

    一.上传文件 上传一个图片 使用input type="file",来上传一个文件.注意:form表单必须添加属性enctype="multipart/form-data ...

随机推荐

  1. ASP.NET——两个下拉框来实现动态联动

    介绍: 在网页中.我们常常会遇到下图中的情况.首先在下拉框中选择所在的省.选择之后,第二个下拉框会自己主动载入出该省中的市.这样设计极大的方便了用户的查找.那这是怎样实现的呢? 1.建立数据库 &qu ...

  2. IC设计前端到后端的流程和eda工具。

    IC前端设计(逻辑设计)和后端设计(物理设计)的区分:以设计是否与工艺有关来区分二者:从设计程度上来讲,前端设计的结果就是得到了芯片的门级网表电路. 前端设计的流程及使用的EDA工具例如以下: 1.架 ...

  3. ASPNETPager常用属性

    <webdiyer:AspNetPager ID="pager" runat="server" class="page" FirstP ...

  4. 什么时候rootViewController至tabbarController时刻,控制屏幕旋转法

    于ios6后,ios系统改变了屏幕旋转的方法,假设您想将屏幕旋转法,在需求rootvc里面制备,例如 UIViewController *viewCtrl = [[UIViewController a ...

  5. Swift初体验(两)

    // 写功能初体验 func getMyName(firstName first:String, lastName last:String) -> String{ //return first ...

  6. Cocos2d-x 3.2 大富翁游戏项目开发-第五部分 单机游戏-级别选择ScrollView

    于MenuScene.cpp 点击单机游戏后会调用 Director::getInstance()->pushScene(MapChooseScene::createScene()); 进入到关 ...

  7. HDU 2616 Kill the monster (暴力搜索 || 终极全阵列暴力)

    主题链接:HDU 2616 Kill the monster 意甲冠军:有N技能比赛HP有M怪物,技能(A,M),能伤害为A.当怪兽HP<=M时伤害为2*A. 求打死怪兽(HP<=0)用的 ...

  8. 64bit Centos6.4编hadoop-2.5.1

    64bit Centos6.4编hadoop-2.5.1   1.说明 a)       因为从apache下载下来的tar.gz包是用32 bit编译的,全部假设用Linux 64作为hadoop的 ...

  9. SVN提交忽略*.class、.classpath、.mymetadata、.project、.settings、.myeclipse和其他非版本控制文件

    1.忽略*.class 在TortoiseSVN -->setting(设定)--规设置 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveHVrZTY ...

  10. 基于protobuf的RPC实现

    可以比较使用google protobuf RPC实现echo service可见.述. google protobuf仅仅负责消息的打包和解包.并不包括RPC的实现.但其包括了RPC的定义.如果有以 ...