将文本文件的编码从utf-8转换为ansi或python中的unicode

时间:2016-12-25 09:50:17

标签: python text encoding utf-8 ansi

我有一个带utf-8编码的文本文件。我想将它的unicode更改为ANSI或在python中自动unicode。 可能吗? 我该怎么办?

2 个答案:

答案 0 :(得分:4)

试试这个

#read input file
with codecs.open('USERS.CSV', 'r', encoding = 'latin-1') as file:
lines = file.read()  

#write output file
with codecs.open('1_UserPython.CSV', 'w', encoding = 'utf_8_sig') as file:
file.write(lines)

答案 1 :(得分:1)

将文件从utf8转换为cp1252:

import io

with io.open(src_path, mode="r", encoding="utf8") as fd:
    content = fd.read()
with io.open(dst_path, mode="w", encoding="cp1252") as fd:
    fd.write(content)