在 python 2.x 中,“编码是无效关键字”错误是不可避免的吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25049962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:39:08  来源:igfitidea点击:

Is 'encoding is an invalid keyword' error inevitable in python 2.x?

pythonpython-2.7python-3.xencodingutf-8

提问by user3123767

Ansi to UTF-8 using python causing error

Ansi 到 UTF-8 使用 python 导致错误

I tried the answer there to convert ansi to utf-8.

我在那里尝试了将 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)

But I got "TypeError: 'encoding' is an invalid keyword argument for this function"

但是我得到了“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.

并且仍然出现相同的错误。我也尝试过 cp1252,但得到了同样的错误。

I learned from several stackoverflow questions that

我从几个stackoverflow问题中了解到

TypeError: 'encoding' is an invalid keyword argument for this function

is frequently arising error message in python 2.x

在python 2.x中经常出现错误信息

But mainly answerers were suggesting using python 3 in some way or the other.

但主要是回答者建议以某种方式使用 python 3。

Is it really impossible to convert ansi txt to utf-8 txt in python 2.x ? (I use 2.7)

在 python 2.x 中将 ansi txt 转换为 utf-8 txt 真的不可能吗?(我使用 2.7)

采纳答案by Rob?

For Python2.7, Use io.open()in both locations.

对于 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)

The above program runs without errors on my PC.

上述程序在我的 PC 上运行没有错误。

回答by Thomas Hobohm

This is how you can convert ansi to utf-8 in Python 2 (you just use normal file objects):

这是在 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"))

回答by Taras Vaskiv

I had the same issue when I did try to write bytes to file. So my point is, bytes are already encoded. So when you use encoding keyword this leads to an error.

当我尝试将字节写入文件时,我遇到了同样的问题。所以我的观点是,字节已经被编码。因此,当您使用 encoding 关键字时,这会导致错误。

回答by Sunku Vamsi Tharun Kumar

TypeError: 'encoding' is an invalid keyword argument for this function

类型错误:“编码”是此函数的无效关键字参数

open('textfile.txt', encoding='utf-16')

Use io, it will work in both 2.7 and 3.6 python version

使用 io,它将在 2.7 和 3.6 python 版本中工作

import io
io.open('textfile.txt', encoding='utf-16')