Google Protocol Buffers List of Lists Python



嗨,我有一个关于json中列表列表的协议缓冲区的问题:

示例。json (testjson)
"outcome": {
"col": [
"datetime",
"return"
],
"val": [[1199232000000, -0.0066], [1199318400000, -0.0033]]
}

My .proto File (cum_ret.proto)

message CumReturn {

message period_value {
oneof types{
int32 day = 1;
float return = 2;
}
}
message period_values {
repeated period_value value = 1;
}
message outcome {
repeated string col = 1;
repeated period_value val = 2;
}
outcome outcome_returns = 2;
}

我用以下代码解析json:

testjson = {
"outcome_returns": {
"col": [
"datetime",
"cum_returns"
],
"val": [[1199232000000, -0.0066705691], [1199318400000, -0.0033641154]]
}
}

import cum_ret_pb2 as CumRet 
from google.protobuf.json_format import Parse
cumrets = Parse(json.dumps(test_json), CumRet.CumReturn())

但是我得到了错误消息:

Failed to parse 1199232000000 field: expected string or bytes-like object...

有人能帮忙吗:获得int和float的列表到.proto模式的列表?

实现列表的列表的一种方法是为列表创建一个新消息:

message ListOfInt {
repeated int32 value = 1;
}

调用时,使用

message outcome {
repeated string col = 1;
repeated ListOfInt val = 2;
}

然而,我认为你有一个不同的问题在你的代码。你的"period_value"Message需要int32或float类型。最大int32值是2,147,483,647,但您试图将1,199,232,000,000放入该字段。因此,出现无法解析该值的错误消息。尝试将int32更改为int64:

message period_value {
oneof types {
int64 day = 1;
float return = 2;
}
}

最新更新