我想要的是非常简单,可以PHP使用一行代码在语言中完成:
file_put_contents('target.txt', iconv('windows-1252', 'utf-8', file_get_contents('source.txt')));
Run Code Online (Sandbox Code Playgroud)
在Python中,我花了一整天时间试图弄清楚如何实现同样的琐碎事情,但无济于事.当我尝试读取或写入文件时,我经常得到UnicodeDecode errors,str has no method decode以及十几个类似的错误.好像我在SO扫描所有线程,但仍然不知道我该怎么做.
你打电话时是否指定了"encoding"关键字参数open?
with open('source.txt', encoding='windows-1252') as f_in:
with open('target.txt', 'w', encoding='utf-8') as f_out:
f_out.write(f_in.read())
Run Code Online (Sandbox Code Playgroud)