简体   繁体   English

使用Python解码protobuf文件时出现一些错误,错误为'google.protobuf.message.DecodeError:解码varint时太多字节。'

[英]I have some errors when decode the protobuf file with Python,error as ‘google.protobuf.message.DecodeError: Too many bytes when decoding varint.’

At first I directly run my decode program, the command is: 起初我直接运行我的解码程序,命令是:

python test_read_protext.py

the error is : 错误是:

Traceback (most recent call last):
  File "test_read_protext.py", line 23, in <module>
    read_protext("buck1")
  File "test_read_protext.py", line 17, in read_protext
    cur_bucket_conf.ParseFromString(str(ret))
  google.protobuf.message.DecodeError: Error parsing message

I have find some some solutions on the web, and I saw some guy add the environment parameter,like PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python, so i run the program as: 我在网上找到了一些解决方案,我看到有些人添加了环境参数,比如PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION = python,所以我运行程序为:

PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python python test_read_protext.py 

but it did't work , the error is: 但它没有用,错误是:

Traceback (most recent call last):
  File "test_read_protext.py", line 23, in <module>
    read_protext("buck1")
  File "test_read_protext.py", line 17, in read_protext
    cur_bucket_conf.ParseFromString(str(ret))
  File "/home/gaoxiang2/.local/lib/python2.7/site-  packages/google/protobuf/message.py", line 187, in ParseFromString
    return self.MergeFromString(serialized)
  File "/home/gaoxiang2/.local/lib/python2.7/site-packages/google/protobuf/internal/python_message.py", line 1124, in MergeFromString
    if self._InternalParse(serialized, 0, length) != length:
  File "/home/gaoxiang2/.local/lib/python2.7/site-packages/google/protobuf/internal/python_message.py", line 1176, in InternalParse
    buffer, new_pos, wire_type)  # pylint: disable=protected-access
  File "/home/gaoxiang2/.local/lib/python2.7/site-packages/google/protobuf/internal/decoder.py", line 948, in _DecodeUnknownField
    (data, pos) = _DecodeUnknownFieldSet(buffer, pos)
  File "/home/gaoxiang2/.local/lib/python2.7/site-packages/google/protobuf/internal/decoder.py", line 927, in _DecodeUnknownFieldSet
    (data, pos) = _DecodeUnknownField(buffer, pos, wire_type)
  File "/home/gaoxiang2/.local/lib/python2.7/site-packages/google/protobuf/internal/decoder.py", line 938, in _DecodeUnknownField
    (data, pos) = _DecodeVarint(buffer, pos)
  File "/home/gaoxiang2/.local/lib/python2.7/site-packages/google/protobuf/internal/decoder.py", line 135, in DecodeVarint
    raise _DecodeError('Too many bytes when decoding varint.')
google.protobuf.message.DecodeError: Too many bytes when decoding varint.

the proto file is : proto文件是:

syntax="proto3";

message StrategyInfo {
    string strategy_ins_name = 1;            
    int32 res_polling_num = 2;              
    int32 strategy_recall_upper_limit = 3;   
};

message SingleBucket {
    string bucket_name = 1;                  
    int32 bucket_priority = 2;               
    int32 bucket_recall_res_proportion = 3; 
    repeated StrategyInfo strategies = 4;    
};

message StrategyBucketConf {
    int32 bucket_num = 1;                    
    repeated SingleBucket buckets = 2;    
};

the real file is like: 真实的文件是这样的:

bucket_num : 1
buckets {
    bucket_name : "hot_class"
    bucket_priority : 0
    bucket_recall_res_proportion : 60
    strategies {
        strategy_ins_name : "short_term_chot_v0"
        res_polling_num : 20
        strategy_recall_upper_limit : 100
    }
    strategies {
        strategy_ins_name : "recall_finish_hot"
        res_polling_num : 20
        strategy_recall_upper_limit : 100
    }
}

the decode program is: 解码程序是:

def read_protext(msg) :
    cur_bucket_conf = bucket_conf.StrategyBucketConf()
    f = open(msg, 'rb')
    ret = f.read()  
    print ret
    cur_bucket_conf.ParseFromString(ret) #read from file
    f.close()
    print cur_bucket_conf.bucket_num

I have solve this question. 我已经解决了这个问题。

instead of 代替

cur_bucket_conf.ParseFromString(ret) 

by 通过

from google.protobuf import text_format
text_format.Parse(ret, cur_bucket_conf)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复:创建 tensorflow 文本摘要时出现“google.protobuf.message.DecodeError: Error parsing message” - How to Fix: "google.protobuf.message.DecodeError: Error parsing message" when creating tensorflow text summary google.protobuf.message.DecodeError:解析消息时出错 - google.protobuf.message.DecodeError: Error parsing message 解析 Google 字体 METADATA.pb - google.protobuf.message.DecodeError: Error parsing message - Parsing Google Fonts METADATA.pb - google.protobuf.message.DecodeError: Error parsing message 在运行optimize_for_inference.py时,如何修复“ google.protobuf.message.DecodeError:错误解析消息” - How to fix 'google.protobuf.message.DecodeError: Error parsing message' while running optimize_for_inference.py Google Protobuf-C ++和Python之间的UDP通信-google.protobuf.message.DecodeError:解压缩需要长度为4的字符串参数 - Google Protobuf - UDP Communication Between C++ and Python - google.protobuf.message.DecodeError: unpack requires a string argument of length 4 google.protobuf.message.DecodeError:协议缓冲区中的标记错误中的线类型错误 - google.protobuf.message.DecodeError: Wrong wire type in tag Error in Protocol Buffer 使用 tensorflow: google.protobuf.message.DecodeError: Wrong wire type in tag - using tensorflow: google.protobuf.message.DecodeError: Wrong wire type in tag 从 Bitbucket 读取字节时 Protobuf 解码错误 - Protobuf Decode Error when reading bytes from withing Bitbucket 将protobuf字节流传输到文件(python) - Stream protobuf bytes to file (python) 解码Google云端存储python API返回的protobuf文件 - decode protobuf file returned by google cloud storage python api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM