为什么用Python替换多行字符串可以处理硬编码字符串,而不是从文件中读取字符串?



我试图用占位符替换字符串的内容,以便以后替换。当我对字符串字面量执行替换时,代码按预期工作,但如果我从文件中读取相同的字符串(实际上是粘贴到文件中的相同字符串字面量),它就不起作用了。

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 serializedn protocol buffer message. This string must contain at leastn one "/" character. The last segment of the URL's path must representn the fully qualified name of the type (as inn `path/google.protobuf.Duration`). The name should be in a canonical formn (e.g., leading "." is not accepted).nn In practice, teams usually precompile into the binary all types that theyn expect it to use in the context of Any. However, for URLs which use then scheme `http`, `https`, or no scheme, one can optionally set up a typen server that maps type URLs to message definitions as follows:nn * 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 then   URL, or have them precompiled into a binary to avoid anyn   lookup. Therefore, binary compatibility needs to be preservedn   on changes to types. (Use versioned type names to managen   breaking changes.)nn Note: this functionality is not currently available in the officialn protobuf release, and it is not used for type URLs beginning withn type.googleapis.com.nn Schemes other than `http`, `https` (or the empty scheme) might ben 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 serializedn protocol buffer message. This string must contain at leastn one "/" character. The last segment of the URL's path must representn the fully qualified name of the type (as inn `path/google.protobuf.Duration`). The name should be in a canonical formn (e.g., leading "." is not accepted).nn In practice, teams usually precompile into the binary all types that theyn expect it to use in the context of Any. However, for URLs which use then scheme `http`, `https`, or no scheme, one can optionally set up a typen server that maps type URLs to message definitions as follows:nn * 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 then   URL, or have them precompiled into a binary to avoid anyn   lookup. Therefore, binary compatibility needs to be preservedn   on changes to types. (Use versioned type names to managen   breaking changes.)nn Note: this functionality is not currently available in the officialn protobuf release, and it is not used for type URLs beginning withn type.googleapis.com.nn Schemes other than `http`, `https` (or the empty scheme) might ben 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>}占位符。

最新更新