你可以使用 Python 的内置函数 open() 和 .encode() 来将 txt 文件编码为 UTF-8。
代码如下:
# 打开 txt 文件,并以读取模式打开
with open('文件路径', 'r', encoding='utf-8') as f:
# 读取文件内容
text = f.read()
# 将文本编码为 UTF-8
text_utf8 = text.encode('utf-8')
# 将编码后的文本写入新的文件
with open('新文件路径', 'w'