Parsing Google Fonts METADATA.pb - google.protobuf.message.DecodeError: Error parsing message
Parsing Google Fonts METADATA.pb - google.protobuf.message.DecodeError: Error parsing message
我正在尝试解析来自官方 Google 字体库的 METADATA.pb 文件,可以在此处找到:https://github.com/google/fonts (Example METADATA.pb file for the Roboto font: https://github.com/google/fonts/blob/master/apache/roboto/METADATA.pb)
要解析 proto-buf 文件,需要正确的格式。可以在此处下载为 "public_fonts.proto":https://github.com/googlefonts/gftools/blob/master/Lib/gftools/fonts_public.proto
我用它生成了一个名为 "fonts_public_pb2.py" 的 Python 代码文件,命令如下:
protoc -I=. --python_out=. fonts_public.proto
这是我的代码,它导入这个生成的文件,读取 METADATA.pb 文件的内容(不管是哪一个,它们都遵循相同的结构)然后尝试解析原型 -缓冲区字符串。
#! /usr/bin/env python
import fonts_public_pb2
protobuf_file_path = 'METADATA.pb'
protobuf_file = open(protobuf_file_path, 'rb')
protobuf = protobuf_file.read()
font_family = fonts_public_pb2.FamilyProto()
font_family.ParseFromString(protobuf)
只有几行,没什么太复杂,但输出总是一样的:
Traceback (most recent call last):
File "parse.py", line 22, in <module>
font_family.ParseFromString(protobuf)
google.protobuf.message.DecodeError: Error parsing message
我通常不在 Python 中编写代码,所以这里的问题很可能是我,但在尝试了一些不同的事情之后我不知道该怎么做了:
- 使用了 gftools 存储库中已经生成的 "fonts_public_pb2.py" 文件:https://github.com/googlefonts/gftools/blob/master/Lib/gftools/fonts_public_pb2.py - 我从 "public_fonts.proto" 文件生成的输出和这个文件几乎相同,我用 Meld 进行了检查。错误还是一样
- 将 .proto 文件中的所有 "required" 字段设置为 "optional",再次生成 "fonts_public_pb2.py" 文件 - 同样的错误
- 已尝试 Python 2 和 3 - 同样的错误
那些 METADATA.pb 文件不是二进制 protobuf 文件,它们使用 text format。
import fonts_public_pb2
from google.protobuf import text_format
protobuf_file_path = 'METADATA.pb'
protobuf_file = open(protobuf_file_path, 'r')
protobuf = protobuf_file.read()
font_family = fonts_public_pb2.FamilyProto()
text_format.Merge(protobuf, font_family)
print(font_family)
我正在尝试解析来自官方 Google 字体库的 METADATA.pb 文件,可以在此处找到:https://github.com/google/fonts (Example METADATA.pb file for the Roboto font: https://github.com/google/fonts/blob/master/apache/roboto/METADATA.pb)
要解析 proto-buf 文件,需要正确的格式。可以在此处下载为 "public_fonts.proto":https://github.com/googlefonts/gftools/blob/master/Lib/gftools/fonts_public.proto
我用它生成了一个名为 "fonts_public_pb2.py" 的 Python 代码文件,命令如下:
protoc -I=. --python_out=. fonts_public.proto
这是我的代码,它导入这个生成的文件,读取 METADATA.pb 文件的内容(不管是哪一个,它们都遵循相同的结构)然后尝试解析原型 -缓冲区字符串。
#! /usr/bin/env python
import fonts_public_pb2
protobuf_file_path = 'METADATA.pb'
protobuf_file = open(protobuf_file_path, 'rb')
protobuf = protobuf_file.read()
font_family = fonts_public_pb2.FamilyProto()
font_family.ParseFromString(protobuf)
只有几行,没什么太复杂,但输出总是一样的:
Traceback (most recent call last):
File "parse.py", line 22, in <module>
font_family.ParseFromString(protobuf)
google.protobuf.message.DecodeError: Error parsing message
我通常不在 Python 中编写代码,所以这里的问题很可能是我,但在尝试了一些不同的事情之后我不知道该怎么做了:
- 使用了 gftools 存储库中已经生成的 "fonts_public_pb2.py" 文件:https://github.com/googlefonts/gftools/blob/master/Lib/gftools/fonts_public_pb2.py - 我从 "public_fonts.proto" 文件生成的输出和这个文件几乎相同,我用 Meld 进行了检查。错误还是一样
- 将 .proto 文件中的所有 "required" 字段设置为 "optional",再次生成 "fonts_public_pb2.py" 文件 - 同样的错误
- 已尝试 Python 2 和 3 - 同样的错误
那些 METADATA.pb 文件不是二进制 protobuf 文件,它们使用 text format。
import fonts_public_pb2
from google.protobuf import text_format
protobuf_file_path = 'METADATA.pb'
protobuf_file = open(protobuf_file_path, 'r')
protobuf = protobuf_file.read()
font_family = fonts_public_pb2.FamilyProto()
text_format.Merge(protobuf, font_family)
print(font_family)