我正在尝试用占位符替换字符串的内容以供以后替换。当我针对字符串文字执行替换时,代码按预期工作,但如果我从文件中读取相同的字符串(实际上是粘贴到文件中的相同字符串文字),它就不起作用。
envelope_string = '''[
{
"name": "created_at",
"type": "TIMESTAMP",
"mode": "NULLABLE",
"description": "Message creation time"
},
{
"name": "payload",
"type": "RECORD",
"mode": "NULLABLE",
"description": "Message payload",
"fields": [
{
"name": "type_url",
"type": "STRING",
"mode": "NULLABLE",
"description": "A URL/resource name that uniquely identifies the type of the serialized\n protocol buffer message. This string must contain at least\n one \"/\" character. The last segment of the URL's path must represent\n the fully qualified name of the type (as in\n `path/google.protobuf.Duration`). The name should be in a canonical form\n (e.g., leading \".\" is not accepted).\n\n In practice, teams usually precompile into the binary all types that they\n expect it to use in the context of Any. However, for URLs which use the\n scheme `http`, `https`, or no scheme, one can optionally set up a type\n server that maps type URLs to message definitions as follows:\n\n * If no scheme is provided, `https` is assumed.\n * An HTTP GET on the URL must yield a [google.protobuf.Type][]\n value in binary format, or produce an error.\n * Applications are allowed to cache lookup results based on the\n URL, or have them precompiled into a binary to avoid any\n lookup. Therefore, binary compatibility needs to be preserved\n on changes to types. (Use versioned type names to manage\n breaking changes.)\n\n Note: this functionality is not currently available in the official\n protobuf release, and it is not used for type URLs beginning with\n type.googleapis.com.\n\n Schemes other than `http`, `https` (or the empty scheme) might be\n used with implementation specific semantics."
},
{
"name": "value",
"type": "BYTES",
"mode": "NULLABLE",
"description": "Must be a valid serialized protocol buffer of the above specified type."
}
]
}
]'''
payload_string = '''[
{
"name": "type_url",
"type": "STRING",
"mode": "NULLABLE",
"description": "A URL/resource name that uniquely identifies the type of the serialized\n protocol buffer message. This string must contain at least\n one \"/\" character. The last segment of the URL's path must represent\n the fully qualified name of the type (as in\n `path/google.protobuf.Duration`). The name should be in a canonical form\n (e.g., leading \".\" is not accepted).\n\n In practice, teams usually precompile into the binary all types that they\n expect it to use in the context of Any. However, for URLs which use the\n scheme `http`, `https`, or no scheme, one can optionally set up a type\n server that maps type URLs to message definitions as follows:\n\n * If no scheme is provided, `https` is assumed.\n * An HTTP GET on the URL must yield a [google.protobuf.Type][]\n value in binary format, or produce an error.\n * Applications are allowed to cache lookup results based on the\n URL, or have them precompiled into a binary to avoid any\n lookup. Therefore, binary compatibility needs to be preserved\n on changes to types. (Use versioned type names to manage\n breaking changes.)\n\n Note: this functionality is not currently available in the official\n protobuf release, and it is not used for type URLs beginning with\n type.googleapis.com.\n\n Schemes other than `http`, `https` (or the empty scheme) might be\n used with implementation specific semantics."
},
{
"name": "value",
"type": "BYTES",
"mode": "NULLABLE",
"description": "Must be a valid serialized protocol buffer of the above specified type."
}
]'''
# This works and replaces the string as expected
my_string = envelope_string.replace(payload_string, "{<Payload>}")
print(my_string)
# But when I read exactly the same text from a file, it doesn't work
f = open("C:\\Temp\\envelope.txt", "r", encoding='utf-8')
file_envelope = f.read()
f.close()
my_file_string = file_envelope.replace(payload_string, "{<Payload>}")
print(my_file_string)
您可以通过简单地将 envelope_string
变量的内容复制到一个文本文件中来进行尝试。我的文本文件的编码是没有签名的UTF-8
如有任何建议,我们将不胜感激。
最佳答案
似乎描述中的换行符阻止了文件数据和字符串文字之间的苹果对苹果比较。解决方案是将要替换的搜索字符串也放入一个文件中,然后将这两个文件读入内存。在 string.replace()
中完成了搜索有效,我成功地用我的 {<Payload>}
替换了 long fields 元素占位符。
关于python - 为什么使用 Python 的多行字符串替换适用于硬编码字符串,但在从文件中读取字符串时却不行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74365128/