从协议缓冲区创建类似python字典的对象,以便在应用程序中使用
我当前与提供协议缓冲区的服务器接口。我可能会收到大量的信息。目前,我读取协议缓冲区并将其转换为Pandas数据帧的过程(通常不是必要的步骤,但Pandas提供了分析数据集的良好工具)是:从协议缓冲区创建类似python字典的对象,以便在应用程序中使用,python,pandas,protocol-buffers,Python,Pandas,Protocol Buffers,我当前与提供协议缓冲区的服务器接口。我可能会收到大量的信息。目前,我读取协议缓冲区并将其转换为Pandas数据帧的过程(通常不是必要的步骤,但Pandas提供了分析数据集的良好工具)是: 读取协议缓冲区,它将是一个google protobuf对象 使用将协议缓冲区转换为字典 使用pandas.DataFrame.from_records获取数据帧 这非常有效,但是,考虑到我从protobuf中读取的大量消息,将其转换为字典然后再转换为pandas是非常低效的。我的问题是:是否有可能使一个类使p
pandas.DataFrame.from_records
获取数据帧这非常有效,但是,考虑到我从protobuf中读取的大量消息,将其转换为字典然后再转换为pandas是非常低效的。我的问题是:是否有可能使一个类使python protobuf对象看起来像一个字典?即,删除步骤2。任何引用或伪代码都会有帮助。您可能需要检查python包。 它确实提供了类似于dict的操作来访问protobuf对象 用法示例: 假设您有一个python protobuf对象
person\u obj
import ProtoText
print person_obj['name'] # print out the person_obj.name
person_obj['name'] = 'David' # set the attribute 'name' to 'David'
# again set the attribute 'name' to 'David' but in batch mode
person_obj.update({'name': 'David'})
print ('name' in person_obj) # print whether the 'name' attribute is set in person_obj
# the 'in' operator is better than the google implementation HasField function
# in the sense that it won't raise Exception even if the field is not defined
但是
将协议缓冲区转换为字典
使python protobuf对象看起来像字典;)您需要一些pandas.DataFrame.from_protbuf,但我不知道这个问题的答案。我看了代码,它看起来肯定不像是在包装protbuf对象,而是创建了一个真正的新字典。我相信@Justin正在寻找只包装而不复制数据的东西。是的,我想让googleprotobuf对象看起来像字典,而不是先将数据复制到python dict。