[英]Access to google protobuf files in python (easy)
I read message from a protobuf file. 我从protobuf文件中读取了消息。 The message contains time-series data, thus, I expected matrix-like structure, eg, processed_row[nData,nVar] 该消息包含时间序列数据,因此,我期望使用类似矩阵的结构,例如,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]
I can access to the individual datum like 我可以像这样访问单个数据
print(mySerializedData.processed_row[0].timestamp)
>> 0.0
print(mySerializedData.processed_row[1].timestamp)
>> 0.0049
But what I'd like to obtain is like 但是我想要获得的是
print(mySerializedData.processed_row[:].timestamp)
But it shows the error AttributeError: 'list' object has no attribute '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'>
Is there way to get get timestamp
double array with like [:]? 有没有办法像[:]那样获得timestamp
双精度数组?
Thank you 谢谢
You can create a list of the timestamp values using a list comprehension : 您可以使用列表推导来创建时间戳记值的列表 :
[element.timestamp for element in mySerializedData.processed_row]
Here's a generalized example. 这是一个概括的例子。 In example.proto
: 在example.proto
:
package example;
message Element {
required double timestamp = 1;
required string data = 2;
}
message Container {
repeated Element elements = 1;
}
Generate the corresponding Python output using: 使用以下命令生成相应的Python输出:
protoc --python_out=. example.proto
Then, in Python: 然后,在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.