google.protobuf.text_format

google.protobuf.text_format#

参考:google.protobuf.text_format

包含用于以文本格式打印协议消息的例程。

简单使用示例:

# Create a proto object and serialize it to a text proto string.
message = my_proto_pb2.MyMessage(foo='bar')
text_proto = text_format.MessageToString(message)

# Parse a text proto string.
message = text_format.Parse(text_proto, my_proto_pb2.MyMessage())

google.protobuf.text_format.MessageToString() 将 protobuf 消息转换为文本格式。

from google.protobuf import text_format
text_format.MessageToString?

Hide code cell output

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 1
----> 1 from google.protobuf import text_format
      2 get_ipython().run_line_magic('pinfo', 'text_format.MessageToString')

ModuleNotFoundError: No module named 'google'

google.protobuf.text_format.Parse() 将协议消息的文本表示解析为消息。

text_format.Parse?

Hide code cell output

Signature:
text_format.Parse(
    text,
    message,
    allow_unknown_extension=False,
    allow_field_number=False,
    descriptor_pool=None,
    allow_unknown_field=False,
)
Docstring:
Parses a text representation of a protocol message into a message.

NOTE: for historical reasons this function does not clear the input
message. This is different from what the binary msg.ParseFrom(...) does.
If text contains a field already set in message, the value is appended if the
field is repeated. Otherwise, an error is raised.

Example::

  a = MyProto()
  a.repeated_field.append('test')
  b = MyProto()

  # Repeated fields are combined
  text_format.Parse(repr(a), b)
  text_format.Parse(repr(a), b) # repeated_field contains ["test", "test"]

  # Non-repeated fields cannot be overwritten
  a.singular_field = 1
  b.singular_field = 2
  text_format.Parse(repr(a), b) # ParseError

  # Binary version:
  b.ParseFromString(a.SerializeToString()) # repeated_field is now "test"

Caller is responsible for clearing the message as needed.

Args:
  text (str): Message text representation.
  message (Message): A protocol buffer message to merge into.
  allow_unknown_extension: if True, skip over missing extensions and keep
    parsing
  allow_field_number: if True, both field number and field name are allowed.
  descriptor_pool (DescriptorPool): Descriptor pool used to resolve Any types.
  allow_unknown_field: if True, skip over unknown field and keep
    parsing. Avoid to use this option if possible. It may hide some
    errors (e.g. spelling error on field name)

Returns:
  Message: The same message passed as argument.

Raises:
  ParseError: On text parsing problems.
File:      /media/pc/data/lxw/envs/anaconda3a/envs/py313/lib/python3.13/site-packages/google/protobuf/text_format.py
Type:      function

注意:由于历史原因,此函数不会清除输入消息。这与二进制 msg.ParseFrom(…) 的行为不同。如果文本中包含消息中已设置的字段,如果该字段是重复的,则追加值;否则,会引发错误。

示例:

a = MyProto()
a.repeated_field.append('test')
b = MyProto()

# Repeated fields are combined
text_format.Parse(repr(a), b)
text_format.Parse(repr(a), b) # repeated_field contains ["test", "test"]

# Non-repeated fields cannot be overwritten
a.singular_field = 1
b.singular_field = 2
text_format.Parse(repr(a), b) # ParseError

# Binary version:
b.ParseFromString(a.SerializeToString()) # repeated_field is now "test"

google.protobuf.text_format.Merge() 将协议消息的文本表示解析为消息。

text_format.Merge?

Hide code cell output

Signature:
text_format.Merge(
    text,
    message,
    allow_unknown_extension=False,
    allow_field_number=False,
    descriptor_pool=None,
    allow_unknown_field=False,
)
Docstring:
Parses a text representation of a protocol message into a message.

Like Parse(), but allows repeated values for a non-repeated field, and uses
the last one. This means any non-repeated, top-level fields specified in text
replace those in the message.

Args:
  text (str): Message text representation.
  message (Message): A protocol buffer message to merge into.
  allow_unknown_extension: if True, skip over missing extensions and keep
    parsing
  allow_field_number: if True, both field number and field name are allowed.
  descriptor_pool (DescriptorPool): Descriptor pool used to resolve Any types.
  allow_unknown_field: if True, skip over unknown field and keep
    parsing. Avoid to use this option if possible. It may hide some
    errors (e.g. spelling error on field name)

Returns:
  Message: The same message passed as argument.

Raises:
  ParseError: On text parsing problems.
File:      /media/pc/data/lxw/envs/anaconda3a/envs/py313/lib/python3.13/site-packages/google/protobuf/text_format.py
Type:      function

google.protobuf.text_format.Parse() 类似,但允许非重复字段有重复值,并使用最后一个值。这意味着文本中指定的任何非重复、顶层字段将替换消息中的字段。