0

早些时候,我问过这个问题: 如何在 Python 3.3 中将某些字符转换为五位 unicode 1?

但是今天我发现大写 U 代码点在打印时有效,但是当我在文件中尝试时,结果却失败了。为什么?

import re

f = codecs.open('test.txt', 'r', encoding="utf-8")
g = codecs.open('test_output.txt', 'w', encoding="utf-8")
fin = f.read()
output = re.sub('m', '\U000243D0', fin)
g.write(output)
4

1 回答 1

1

这对我来说很好:

import re

with open('/tmp/test.txt', 'w', encoding='utf8') as testfile:
    testfile.write("I don't go to school on mondays")

with open('/tmp/test.txt', 'r', encoding='utf8') as testfile, open('/tmp/test_output.txt', 'w', encoding='utf8') as testout:
    output = re.sub('m', '\U000243D0', testfile.read())
    testout.write(output)

with open('/tmp/test_output.txt', 'r', encoding='utf8') as testfile:
    print(repr(testfile.read()))

输出

"I don't go to school on ondays"
于 2013-02-05T13:31:31.497 回答