use*_*767 39 python encoding utf-8 python-2.7 python-3.x
我在那里尝试了将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)
Rob*_*obᵩ 64
对于Python2.7,请io.open()
在两个位置使用.
import io
import shutil
with io.open('/etc/passwd', encoding='latin-1', errors='ignore') as source:
with io.open('/tmp/goof', mode='w', encoding='utf-8') as target:
shutil.copyfileobj(source, target)
Run Code Online (Sandbox Code Playgroud)
上面的程序在我的电脑上运行没有错误.
Tho*_*ohm 12
这是你在Python 2中将ansi转换为utf-8的方法(你只需使用普通的文件对象):
with open(file_path_ansi, "r") as source:
with open(file_path_utf8, "w") as target:
target.write(source.read().decode("latin1").encode("utf8"))
Run Code Online (Sandbox Code Playgroud)
小智 6
类型错误:“编码”是此函数的无效关键字参数
open('textfile.txt', encoding='utf-16')
Run Code Online (Sandbox Code Playgroud)
使用 io,它将在 2.7 和 3.6 python 版本中工作
import io
io.open('textfile.txt', encoding='utf-16')
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
72941 次 |
最近记录: |