字节(str?)到CSV

2024-10-01 17:30:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我看过了here和{a2}的帖子。但他们要么试图将字节写入CSV,要么从字符串中保存CSV,但我正在尝试将尝试写入字节的CSV连接起来。在

我有一个bytes的blob,如果我type(bytes)我得到{},但是当我试图写入csv时,我得到了一个错误

(Pdb) f = open('/Users/minhmai/test.csv', 'wb+')
(Pdb) writer = csv.writer(f)
(Pdb) writer.writerow(bytes)
*** TypeError: a bytes-like object is required, not 'str'


(Pdb) f = open('/Users/minhmai/test.csv', 'wb+')
(Pdb) writer = csv.writer(f)
(Pdb) writer.writerows(bytes)
*** _csv.Error: iterable expected, not int

但是,如果我这样做,我只得到一个数字,但它不是真正保存到CSV。在

^{pr2}$

我的数据(bytes)的示例如下

b'Date,ID,Amount\r\n2018-1-1,1,25\r\n2018-1-2,2,3\r\n2018-1-1,2,3\r\n`

理想情况下,这将导致一个标题行和三行


Tags: csvtesta2字节bytesherenotopen

热门问题