Search code examples
python-3.xfilecsviobyte

read csv in rb mode and and convert to a list of list of strings in python3


I have a csvfile that gets passed in a binary mode only (rb). Fhe file object already gets read in this mode and that's how it gets passed. I want to convert that to a a list of lists.

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

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

I want to convert this to strings

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

how do I get to do this in python3. I think i get errors like

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

How can I change the file read in bytes to strings or perhaps write into csv ?


Solution

  • I was able to read the file in python 3 as below:

    df = pandas.read_csv(f.stream)
    

    Ref to link:

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