Bytes(str?) to CSV

241
January 13, 2018, at 05:07 AM

I've looked at the posts here and here. But they have either been trying to write bytes to a CSV or saving a CSV from a string but I'm trying to connect trying writing bytes to a CSV.

I have a blob of bytes and if I do type(bytes) I get <class 'bytes'> but when I try to write to a csv I get an error

(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

However If I do this, I just get a number but it's not really saved to a CSV.

(Pdb) wfile = io.StringIO()
(Pdb) writer = csv.writer(wfile)
(Pdb) writer.writerow(bytes)

And example of what my data(bytes) look like is this

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

Ideally this would result in a header row and 3 rows

READ ALSO
Python, is there a way to assign df.drop to a new variable?

Python, is there a way to assign df.drop to a new variable?

I've been practicing python for a while now and just got into pandas to start learning dataframesI understand that df

187
TypeError: method missing required positional argument

TypeError: method missing required positional argument

This is a simplified version of the script I'm working on, in order to be posted here

247
Conda won&#39;t activate any environment since update

Conda won't activate any environment since update

I updated conda and now I have an error not easily found on the web or stackoverflow

157