【发布时间】:2014-09-13 14:46:30
【问题描述】:
当我尝试编写一个将 Ansi 转换为 UTF-8 的 python 程序时,我发现了这个
https://stackoverflow.com/questions/14732996/how-can-i-convert-utf-8-to-ansi-in-python
将 UTF-8 转换为 Ansi。
我认为只要颠倒顺序就可以了。所以我编码了
file_path_ansi = "input.txt"
file_path_utf8 = "output.txt"
#open and encode the original content
file_source = open(file_path_ansi, mode='r', encoding='latin-1', errors='ignore')
file_content = file_source.read()
file_source.close
#write
file_target = open(file_path_utf8, mode='w', encoding='utf-8')
file_target.write(file_content)
file_target.close
但它会导致错误。
TypeError: file<> takes at most 3 arguments <4 given>
所以我改变了
file_source = open(file_path_ansi, mode='r', encoding='latin-1', errors='ignore')
到
file_source = open(file_path_ansi, mode='r', encoding='latin-1')
然后它会导致另一个错误:
TypeError: 'encoding' is an invalid keyword arguemtn for this function
我应该如何修复我的代码来解决这个问题?
【问题讨论】:
标签: python utf-8 character-encoding