I am trying to replace the contents of a string with a placeholder for later substitutions. When I execute my replacement against a string literal, the code works as expected, but if I read the same string from a file (literally the same string literal pasted into the file), it doesn't work.
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)
You can try this by simply copying the contents of the envelope_string
variable into a text file. The encoding for my text file is UTF-8 without signature
Any suggestions are gratefully received.