在protobuf中使用python的extension

本文介绍如何使用C++编写.pb文件,并将其转换为XML格式。通过直接解析.pb文件内容,可以将其输出为文本形式,进而转换为所需的XML格式。
现在情况是使用c++写了.pb文件,而我需要读.pb文件并且重写为xml文件,在用c++书写文件时使用了extension函数,而python中没有对应c++中的GetExtension()函数,因而无从下手(经过测试HasExtension(self,extension)中的extension为你所import的模块.extension的名字)。但最后发现直接进行读取address_book.ParseFromString(f.read())然后直接print address_book就可以打印出所有内容,此时已txt文本作为打印内容的输出,然后进行文本内容的处理就行了:)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-35-b42d4f2a0683> in <module> 1 import pandas as pd 2 import numpy as np ----> 3 import tensorflow as tf 4 5 # 1. 加载CSV文件(针对指定路径) /usr/local/lib/python3.7/site-packages/tensorflow/__init__.py in <module> 22 23 # pylint: disable=g-bad-import-order ---> 24 from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import 25 26 from tensorflow._api.v1 import app /usr/local/lib/python3.7/site-packages/tensorflow/python/__init__.py in <module> 50 51 # Protocol buffers ---> 52 from tensorflow.core.framework.graph_pb2 import * 53 from tensorflow.core.framework.node_def_pb2 import * 54 from tensorflow.core.framework.summary_pb2 import * /usr/local/lib/python3.7/site-packages/tensorflow/core/framework/graph_pb2.py in <module> 13 14 ---> 15 from tensorflow.core.framework import node_def_pb2 as tensorflow_dot_core_dot_framework_dot_node__def__pb2 16 from tensorflow.core.framework import function_pb2 as tensorflow_dot_core_dot_framework_dot_function__pb2 17 from tensorflow.core.framework import versions_pb2 as tensorflow_dot_core_dot_framework_dot_versions__pb2 /usr/local/lib/python3.7/site-packages/tensorflow/core/framework/node_def_pb2.py in <module> 13 14 ---> 15 from tensorflow.core.framework import attr_value_pb2 as tensorflow_dot_core_dot_framework_dot_attr__value__pb2 16 17 /usr/local/lib/python3.7/site-packages/tensorflow/core/framework/attr_value_pb2.py in <module> 13 14 ---> 15 from tensorflow.core.framework import tensor_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__pb2 16 from tensorflow.core.framework import tensor_shape_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__shape__pb2 17 from tensorflow.core.framework import types_pb2 as tensorflow_dot_core_dot_framework_dot_types__pb2 /usr/local/lib/python3.7/site-packages/tensorflow/core/framework/tensor_pb2.py in <module> 13 14 ---> 15 from tensorflow.core.framework import resource_handle_pb2 as tensorflow_dot_core_dot_framework_dot_resource__handle__pb2 16 from tensorflow.core.framework import tensor_shape_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__shape__pb2 17 from tensorflow.core.framework import types_pb2 as tensorflow_dot_core_dot_framework_dot_types__pb2 /usr/local/lib/python3.7/site-packages/tensorflow/core/framework/resource_handle_pb2.py in <module> 39 message_type=None, enum_type=None, containing_type=None, 40 is_extension=False, extension_scope=None, ---> 41 serialized_options=None, file=DESCRIPTOR), 42 _descriptor.FieldDescriptor( 43 name='container', full_name='tensorflow.ResourceHandleProto.container', index=1, /usr/local/lib/python3.7/site-packages/google/protobuf/descriptor.py in __new__(cls, name, full_name, index, number, type, cpp_type, label, default_value, message_type, enum_type, containing_type, is_extension, extension_scope, options, serialized_options, has_default_value, containing_oneof, json_name, file, create_key) 559 has_default_value=True, containing_oneof=None, json_name=None, 560 file=None, create_key=None): # pylint: disable=redefined-builtin --> 561 _message.Message._CheckCalledFromGeneratedFile() 562 if is_extension: 563 return _message.default_pool.FindExtensionByName(full_name) TypeError: Descriptors cannot not be created directly. If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0. If you cannot immediately regenerate your protos, some other possible workarounds are: 1. Downgrade the protobuf package to 3.20.x or lower. 2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower). More information: https://developers.google.com/protocol-buffers/docs/news/2022-05-06#python-updates
最新发布
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值