Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何解析protobuf文件中缺少引号的字符串字段?_Python_Parsing_Protocol Buffers - Fatal编程技术网

Python 如何解析protobuf文件中缺少引号的字符串字段?

Python 如何解析protobuf文件中缺少引号的字符串字段?,python,parsing,protocol-buffers,Python,Parsing,Protocol Buffers,我试图解析一个protobuf文件,其中一个字段是一个没有引号的字符串。因此,我使用的解析器将该字段读取为int。我希望提取实际字符串 在下面的示例中,如何将此字段类型解析为字符串卷积而不是4 >>> import numpy as np >>> import sys, os >>> import argparse >>> import caffe_quant_pb2 as cq >>> from goog

我试图解析一个protobuf文件,其中一个字段是一个没有引号的字符串。因此,我使用的解析器将该字段读取为int。我希望提取实际字符串

在下面的示例中,如何将此字段类型解析为字符串卷积而不是4

>>> import numpy as np
>>> import sys, os
>>> import argparse
>>> import caffe_quant_pb2 as cq
>>> from google.protobuf import text_format
>>> f = open('models/vgg/deploy.prototxt', 'r')
>>> net_txt = cq.NetParameter()
>>> text_format.Parse(f.read(), net_txt)
name: "VGG_ILSVRC_16_layers"
layers {
  bottom: "data"
  top: "conv1_1"
  name: "conv1_1"
  type: CONVOLUTION
  convolution_param {
    num_output: 64
    pad: 1
    kernel_size: 3
  }
}
>>> print net_txt.layers[0].name  # works as I expect
conv1_1
>>> print net_txt.layers[0].type  # reads CONVOLUTION as the 'int' 4
4
>>> print type(net_txt.layers[0].type)
<type 'int'>
>>> print str("CONVOLUTION" == net_txt.layers[0].type)
False
>>> print str(net_txt.layers[0].type)
4

您正在尝试读取其中的枚举:

转换为字符串可能不是最优雅的方式

我认为这应该是真的:

print str(cq.V1LayerParameter.LayerType.DESCRIPTOR.values_by_name["CONVOLUTION"].number == net_txt.layers[0].type)

啊!!!我没有意识到这是一个枚举。谢谢你,我可以接受你提出的解决方案。我必须做更多的挖掘,以避免使用字段本身来提取枚举值,因此在这里共享:>>>打印cq.V1LayerParameter.LayerType.DESCRIPTOR.values\u by_name[coulsion]。第4号