当我试图编写一个将Ansi转换为UTF-8的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
我应该如何修改代码来解决这个问题?