在python 2.x中,'encoding是无效的关键字'错误是不可避免的吗? [英] Is 'encoding is an invalid keyword' error inevitable in python 2.x?
问题描述
我尝试了将ansi转换为utf-8的答案。
with io.open(file_path_ansi,encoding ='latin-1',errors ='ignore')as source:
with open(file_path_utf8,mode ='w' ,encoding ='utf-8')作为目标:
shutil.copyfileobj(来源,目标)
我尝试使用
$ b $ io.open(file_path_ansi,encoding ='cp1252',errors ='ignore')作为来源:
也有同样的错误。
然后我尝试了
import io
with io.open(file_path_ansi,encoding ='latin-1',errors ='ignore')as source:
与io.open(file_path_utf8 ,mode ='w',encoding ='utf-8')作为目标:
shutil.copyfileobj(源,目标)
仍然有相同的错误。我也试过cp1252,但也有同样的错误。
我从几个stackoverflow问题中学到了
TypeError:'encoding '是这个函数的无效关键字参数
在python 2.x中经常出现错误消息
但主要是回答者建议使用python 3以某种方式或其他方式。
真的不可能转换ansi txt到utf-8 txt在python 2.x? (我使用2.7)
对于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' ='utf-8')作为目标:
shutil.copyfileobj(来源,目标)
上述程序在我的电脑上运行没有错误。
Ansi to UTF-8 using python causing error
I tried the answer there to convert ansi to 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)
But I got "TypeError: 'encoding' is an invalid keyword argument for this function"
I tried with
with io.open(file_path_ansi, encoding='cp1252', errors='ignore') as source:
, too, and got same error.
Then I tried
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)
and still got the same error. Also I tried with cp1252, too, but got the same error.
I learned from several stackoverflow questions that
TypeError: 'encoding' is an invalid keyword argument for this function
is frequently arising error message in python 2.x
But mainly answerers were suggesting using python 3 in some way or the other.
Is it really impossible to convert ansi txt to utf-8 txt in python 2.x ? (I use 2.7)
For Python2.7, Use io.open()
in both locations.
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)
The above program runs without errors on my PC.
这篇关于在python 2.x中,'encoding是无效的关键字'错误是不可避免的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!