我在那里尝试了将ansi转换为utf-8的答案.
import io
with io.open(file_path_ansi, encoding='latin-1', errors='ignore') as source:
with open(file_path_utf8, mode='w', encoding='utf-8') as target:
shutil.copyfileobj(source, target)
Run Code Online (Sandbox Code Playgroud)
但我得到"TypeError:'encoding'是这个函数的无效关键字参数"
我试过了
with io.open(file_path_ansi, encoding='cp1252', errors='ignore') as source:
Run Code Online (Sandbox Code Playgroud)
也是,并得到同样的错误.
然后我试了一下
import io
with io.open(file_path_ansi, encoding='latin-1', errors='ignore') as source:
with io.open(file_path_utf8, mode='w', encoding='utf-8') as target:
shutil.copyfileobj(source, target)
Run Code Online (Sandbox Code Playgroud)
仍然有同样的错误.我也试过cp1252,但得到了同样的错误.
我从几个stackoverflow问题中学到了这些
TypeError: 'encoding' is an invalid keyword argument for this function
Run Code Online (Sandbox Code Playgroud)
在python 2.x中经常出现错误消息
但主要是回答者建议以某种方式使用python 3.
在python 2.x中将ansi txt转换为utf-8 txt真的不可能吗?(我用2.7)