在Python中将Unicode转换为ANSI或UTF8

0 投票
1 回答
3069 浏览
提问于 2025-04-20 09:36

我正在使用 Python 2.7.8,并且我有一个用来解析的脚本:

def parse():
    with open('myfile.txt') as f:
.
.
.
Print l

myfile.txt 文件是用 UNICODE 编码的。 我该如何在这个脚本中添加代码,让它把 myfile.txt 当作 ANSI 编码来读取呢?

1 个回答

2

使用io解决了我的问题:

import io    
def parse()
    with io.open(path,'r',encoding='utf_16') as f:
.....

解决了我的问题。

撰写回答