从包含混合 unicode 字符的文件中读取并替换字符串 (python)
Read from file with mixed unicode characters and replace string (python)
文件内容如下:
H\u00f6gskolan
Högskolan
(注:\u00f6
其实是ö
)
我想用 o
替换所有 ö
个字符,并将其保存到新文件中。
常规字符串替换未按预期工作。
with open("test.txt", encoding="utf-8") as f:
content = f.read()
replacedContent = content.replace("ö", "o")
with open("testOutput.txt", "w", encoding="utf-8") as f:
f.write(replacedContent)
testOutput.txt文件内容:
H\u00f6gskolan
Hogskolan
我找到了一个 similar question 但它的解决方案也不起作用:
import codecs
with open("test.txt", encoding="utf-8") as f:
content = f.read()
content = codecs.decode(content, "unicode_escape")
replacedContent = content.replace("ö", "o")
with open("testOutput.txt", "w", encoding="utf-8") as f:
f.write(replacedContent)
testOutput.txt文件内容:
Hogskolan
Högskolan
知道如何实现吗?
我找不到任何已经处理您请求的库函数,所以我只是用正则表达式解析原始 unicode 签名并用它们的实际值替换它们
import re
def convert_mixed(from_file, to_file):
f = open(from_file, "r")
content = f.read()
f.close()
for x in re.findall(r"\u([\da-f]{4})", content):
content = content.replace(f"\u{x}", chr(int(x, 16)))
f = open(to_file, "w")
f.write(content.replace("ö", "o"))
convert_mixed("file.txt", "out.txt")
文件内容如下:
H\u00f6gskolan
Högskolan
(注:\u00f6
其实是ö
)
我想用 o
替换所有 ö
个字符,并将其保存到新文件中。
常规字符串替换未按预期工作。
with open("test.txt", encoding="utf-8") as f:
content = f.read()
replacedContent = content.replace("ö", "o")
with open("testOutput.txt", "w", encoding="utf-8") as f:
f.write(replacedContent)
testOutput.txt文件内容:
H\u00f6gskolan
Hogskolan
我找到了一个 similar question 但它的解决方案也不起作用:
import codecs
with open("test.txt", encoding="utf-8") as f:
content = f.read()
content = codecs.decode(content, "unicode_escape")
replacedContent = content.replace("ö", "o")
with open("testOutput.txt", "w", encoding="utf-8") as f:
f.write(replacedContent)
testOutput.txt文件内容:
Hogskolan
Högskolan
知道如何实现吗?
我找不到任何已经处理您请求的库函数,所以我只是用正则表达式解析原始 unicode 签名并用它们的实际值替换它们
import re
def convert_mixed(from_file, to_file):
f = open(from_file, "r")
content = f.read()
f.close()
for x in re.findall(r"\u([\da-f]{4})", content):
content = content.replace(f"\u{x}", chr(int(x, 16)))
f = open(to_file, "w")
f.write(content.replace("ö", "o"))
convert_mixed("file.txt", "out.txt")