Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:没有字符串参数的编码或错误_Python_Python 3.x - Fatal编程技术网

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

Python TypeError:没有字符串参数的编码或错误,python,python-3.x,Python,Python 3.x,我正在尝试将数据字节列表写入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')) 但它会导致以下错误: TypeError:没有字符串参数的编码或错误 datas是字节字符串的列表 print(dat

我正在尝试将数据字节列表写入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'))
但它会导致以下错误:

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

datas
是字节字符串的列表

print(datas)
屈服

[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]

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

您的
数据
似乎已经是字节格式,所以要将其转换为UTF-8字符串,您必须使用
str
,而不是
字节
!此外,您必须单独转换
数据中的每个元素,而不是一次转换整个列表。最后,如果要将
数据
作为一行添加到
out.csv
,则必须使用
writerow
,而
writerows
将一次写入所有行,因此需要一个列表

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

这似乎是你想要的。结果是一个CSV文件,其中有一行UTF-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) 请注意,
datas
中的最后一项包含一些换行符,因此将拆分为几行。这可能不是你想要的。或者这是您的
数据列表中的一个小故障?

此错误只是意味着您要传递给
字节的内容(要转换为字节序列的字符串)实际上不是字符串。它并不特别意味着参数已经是类型
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
字节(b'',编码=“utf-8”) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 TypeError:编码时没有字符串参数 >>>字节(无,编码=“utf-8”) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 TypeError:编码时没有字符串参数 >>>字节(12,encoding=“utf-8”) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 TypeError:编码时没有字符串参数
既然您想将字节转换回字符串,那么它不应该是
str(datas,'UTF-8')
?(还有,你不应该把它应用到每个元素,而不是整个列表吗?)另外,对于
writerows
datas
不应该是列表列表吗?@tobias_k shows
TypeError:compressing to str:需要一个像object这样的字节,list find
,我仍然不确定我真的理解你想做什么。请您发布您正在使用的确切的
数据,以及该数据在CSV中的显示方式好吗?(在
数据中缺少一个
,因此这似乎被裁剪了。)另外,
\n
应该如何在CSV中呈现,这应该是一行还是多行?附录:如果你想解码unicode但保持
\n
转义,你可以尝试
repr(str(d,'UTF-8')
,但我不确定这是否是你想要的。这也将在
中包装所有字符串。“
尝试过,但它显示
类型错误:“str”不支持缓冲区接口
@AvinashRaj我得到了这个错误,通过将文件模式从
wb
更改为
w
来修复它。(我认为
b
(二进制)对于CSV文件来说没有意义)对不起,忘了提到那个更改。它让我发疯。如果我删除了
wb
中的
b
,它将显示
UnicodeEncodeError:'charmap'编解码器无法对字符'\u221e'进行编码。
。它无法将unicode字符转换为字符串。如果您有时间,请跳转到。我要为此悬赏。。