我用Python解码protobuf文件时发生错误,错误为'google.protobuf.message.DecodeError: Too many bytes when decoding varint.'

2024-04-27 15:33:57 发布

您现在位置:Python中文网/ 问答频道 /正文

一开始我直接运行解码程序 命令是:

python test_read_protext.py

错误是:

^{pr2}$

我在网上找到了一些解决方案,我看到有人添加了环境参数,比如PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=PYTHON,所以我将程序运行为:

PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python python test_read_protext.py 

但没用,错误是:

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.

原型文件是:

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;    
};

真正的文件是这样的:

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
    }
}

解码程序是:

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

Tags: inpyhomereadbucketlibpackageslocal