在rb模式下读取csv并转换为python3中的字符串列表列表

时间:2018-03-14 16:38:58

标签: python-3.x file csv io byte

我有一个仅以二进制模式传递的csvfile(rb)。已经在此模式下读取了文件对象,以及它是如何传递的。我想将其转换为列表列表。

a,b,c
d,e,f
g,h,i

file=open('data.csv','rb')

我想将其转换为字符串

[[a,b,c],[d,e,f],[g,h,i]]

如何在python3中执行此操作。我想我会遇到像

这样的错误
_csv.Error: Iterator should return strings, not bytes( did you open the file in text mode?)

如何将以字节为单位的文件更改为字符串,或者写入csv?

1 个答案:

答案 0 :(得分:1)

我能够在python 3中读取文件,如下所示:

df = pandas.read_csv(f.stream)

参考链接:

[https://www.reddit.com/r/learnpython/comments/5t7h3p/opening_an_uploaded_file_in_flask/][1]