Python 类型错误:没有字符串参数的编码或错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30712020/
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-19 08:51:20  来源:igfitidea点击:

TypeError: encoding or errors without a string argument

pythonpython-3.x

提问by Avinash Raj

I'm trying to write a list of data bytes to a CSV file. Since it's a list of byte strings, I used the below code:

我正在尝试将数据字节列表写入 CSV 文件。由于它是字节字符串列表,因此我使用了以下代码:

with open(r"E:\Avinash\Python\extracting-drug-data\out.csv", "wb") as w:
    writer = csv.writer(w)
    writer.writerows(bytes(datas, 'UTF-8'))

But it results in the following error:

但它导致以下错误:

TypeError: encoding or errors without a string argument

类型错误:没有字符串参数的编码或错误

datasis a list of byte strings.

datas是一个字节串列表。

print(datas)

yields

产量

[b'DB08873', b' MOLSDFPDBSMILESInChIView Structure \xc3\x97Structure for DB08873 (Boceprevir) Close', b'394730-60-0', b'LHHCSNFAOIFYRV-DOVBMPENSA-N', b'Organic acids and derivatives  ', b'Food increases exposure of boceprevir by up to 65% relative to fasting state. However, type of food and time of meal does not affect bioavailability of boceprevir and thus can be taken without regards to food.  \r\nTmax = 2 hours;\r\nTime to steady state, three times a day dosing = 1 day;\r\nCmax]

I want the above list to be printed as first row in a CSV file with the decoding of Unicode chars. That is, \xc3\x97should be converted to it's corresponding character.

我希望将上面的列表作为 CSV 文件中的第一行打印,并解码 Unicode 字符。也就是说,\xc3\x97应该转换为它的相应字符。

采纳答案by tobias_k

It seems your datasisalready in bytes format, so to turn it into UTF-8 strings, you have to use str, not bytes! Also, you have to convert each element from datasindividually, not the entire list at once. Finally, if you want to add datasas one row to out.csv, you have to use writerow, whereas writerowswould write allthe rows at once, and accordinly would expect a list of lists.

看来你datas已经以字节为单位的格式,所以把它变成UTF-8字符串,你要使用str,不bytes!此外,您必须datas单独转换每个元素,而不是一次转换整个列表。最后,如果您想将datas一行添加到out.csv,则必须使用writerow,而writerows将一次写入所有行,并且相应地需要一个列表列表。

Depending on your OS, you might also have to specify the encodingwhen opening the file. Otherwise it will use the OS' default encoding, which might be something entirely different.

根据您的操作系统,您可能还必须encoding在打开文件时指定。否则它将使用操作系统的默认编码,这可能是完全不同的。

This seemsto do what you want. The result is a CSV file with one row1of data in UTF-8 format, and the \xc3\x97is decoded to ×.

似乎做你想做的。结果是一个 CSV 文件,其中包含一行1UTF-8 格式的数据,并且\xc3\x97解码为×.

import csv
with open(r"out.csv", "w", encoding='UTF-8') as w:
    writer = csv.writer(w)
    writer.writerow([str(d, 'UTF-8') for d in datas])


1)Note that the last item in datascontains some line breaks, and thus will be split onto several lines. This is probably not what you want. Or is this a glitch in your dataslist?

1)请注意,最后一项datas包含一些换行符,因此将分为几行。这可能不是您想要的。或者这是您datas列表中的一个小故障?

回答by Hyman M

This error just means the thing you're passing to bytes(the string you want converted to a byte sequence) is not in fact a string. It does notspecifically mean that the argument is already of type bytes, just that it isn'ta string.

这个错误只是意味着你传递给的东西bytes(你想转换为字节序列的字符串)实际上不是一个字符串。它并没有明确表示该说法已经是类型的bytes,只是它不是一个字符串。

>>> bytes(b"", encoding="utf-8")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: encoding without a string argument
>>> bytes(None, encoding="utf-8")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: encoding without a string argument
>>> bytes(12, encoding="utf-8")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: encoding without a string argument