I declare a BytesIO()
object to write through csv.writer
as:
lines = ["abc", "def", "ghi"]
writer_file = io.BytesIO()
writer = csv.writer(writer_file)
writer.writerow(str(lines).encode("utf-8"))
but I get an error:
TypeError: a bytes-like object is required, not 'str'
but when I check the types, I get a bytes
object.
In [14]: type(str(lines).encode("utf-8"))
Out[14]: bytes
What does this mean?
Note: I can't use io.StringIO()
to keep this code compatible for python2.
(Basing my assumptions on this: different io types for csv in python 2 and 3 )