python讀寫protobuf

From: http://blog.sina.com.cn/s/blog_7575a6190101u86f.html


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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章