[英]How to access variable of type google.protobuf.Timestamp in python
[英]Access to google protobuf files in python (easy)
我从protobuf文件中读取了消息。 该消息包含时间序列数据,因此,我期望使用类似矩阵的结构,例如,processed_row [nData,nVar]
print(mySerializedData.processed_row)
>> [timestamp: 0.0
linear_acc_x: 0.288501300049
linear_acc_y: 0.573411297607
linear_acc_z: 0.161608612061
, timestamp: 0.0049
linear_acc_x: 0.428562097168
linear_acc_y: 0.685938775635
linear_acc_z: 0.221463653564
, timestamp: 0.01
linear_acc_x: 0.45968671875
linear_acc_y: 0.738611212158
linear_acc_z: 0.185550628662]
我可以像这样访问单个数据
print(mySerializedData.processed_row[0].timestamp)
>> 0.0
print(mySerializedData.processed_row[1].timestamp)
>> 0.0049
但是我想要获得的是
print(mySerializedData.processed_row[:].timestamp)
但是它显示错误AttributeError: 'list' object has no attribute 'timestamp'
print(type(mySerializedData.processed_row[0].timestamp))
>> <type 'float'>
print(type(mySerializedData.processed_row[0]))
>> <class 'PushCore_pb2.ProcessedDataRow'>
有没有办法像[:]那样获得timestamp
双精度数组?
谢谢
您可以使用列表推导来创建时间戳记值的列表 :
[element.timestamp for element in mySerializedData.processed_row]
这是一个概括的例子。 在example.proto
:
package example;
message Element {
required double timestamp = 1;
required string data = 2;
}
message Container {
repeated Element elements = 1;
}
使用以下命令生成相应的Python输出:
protoc --python_out=. example.proto
然后,在Python中:
>>> from example_pb2 import Container, Element
>>>
>>> container = Container(
... elements=[
... Element(timestamp=.1, data='Some data.'),
... Element(timestamp=.2, data='Some more data.'),
... ]
... )
>>>
>>> [element.timestamp for element in container.elements]
[0.1, 0.2]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.