python下使用protobuf


python解決ImportError: No module named google.protobuf
  關於protocol buffer的優點,就過多涉及;如果涉及到數據傳輸和解析,使用pb會比自己去寫解析代碼更有效率,至少對於大部分而言是這樣的。

一、下載,安裝

  到code.google.com下載源碼,解壓:

    ./configure && make && make check && make install
    最后一步涉及到權限,可能會需要sudo。
二、定義一個proto文件

  下面依然是給出一個簡單的例子,要使用proto首先需要自己定義一個proto文件,定義一個people.proto文件,內容如下:
  message people
  {
      optional string name = 1;
      optional int32 height = 2;
  }

三、生成一個python可用的py文件

  然后就是生成對應的py文件,命令如下:
    protoc -I=./ --python_out=./ people.proto
其中-I是source的路徑,--python_out表示對應python庫的生成路徑,然后是對應的proto文件。當然,pb還支持c++和java,修改--python_out即可。
完成后就有對應的people_pb2.py文件了。導入后即可使用,第一次安裝后直接用應該會提示:ImportError: No module named google.protobuf,這是因為找不到對應的庫路徑導致,到你下載的pb路徑下,找到python路徑,執行sudo python setup.py install,執行完后可以通過執行sudo python setup.py test檢查是否有安裝成功,如果最后提示
----------------------------------------------------------------------
Ran 193 tests in 0.327s
OK
那么就是安裝成功了,此時再導入對應的pb2.py文件即可使用。
--------------------------------------------------------------------------------------------------------------------------------------------------------
執行python setup.py install 時可能有:This script requires setuptools version 0.6c11 to run
可從:http://download.csdn.net/download/fhqsse220/5602687下載
gzip -d setuptools-0.6c11.tar.gz
tar xf setuptools-0.6c11.tar
cd setuptools-0.6c11python setup.py install
返回如下則表示安裝成功
Installed /usr/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
Processing dependencies for setuptools==0.6c11
Finished processing dependencies for setuptools==0.6c11
----------------------------------------------------------------------------------------------
繼續報錯:error: package directory 'google/protobuf/compiler' does not exist
解決辦法:在當前目錄下的google/protobuf/下創建compiler文件夾
再次在protobuf文件夾下運行:python setup.py 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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM