Google 列表的协议缓冲区列表 Python
Google Protocol Buffers List of Lists Python
您好,我有一个关于 json 中列表列表的协议缓冲区的问题:
示例.json(测试json)
"outcome": {
"col": [
"datetime",
"return"
],
"val": [[1199232000000, -0.0066], [1199318400000, -0.0033]]
}
我的 .proto 文件 (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”消息需要一个 int32 或一个浮点数。最大 int32 值是 2,147,483,647,但您试图将 1,199,232,000,000 放入该字段。因此,无法解析该值的错误消息。尝试将 int32 更改为 int64:
message period_value {
oneof types {
int64 day = 1;
float return = 2;
}
}
您好,我有一个关于 json 中列表列表的协议缓冲区的问题:
示例.json(测试json)
"outcome": {
"col": [
"datetime",
"return"
],
"val": [[1199232000000, -0.0066], [1199318400000, -0.0033]]
}
我的 .proto 文件 (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”消息需要一个 int32 或一个浮点数。最大 int32 值是 2,147,483,647,但您试图将 1,199,232,000,000 放入该字段。因此,无法解析该值的错误消息。尝试将 int32 更改为 int64:
message period_value {
oneof types {
int64 day = 1;
float return = 2;
}
}