I am trying to write python 2/3 compatible code to write strings to csv file object. This code:
line_as_list = [line.encode() for line in line_as_list]
writer_file = io.BytesIO()
writer = csv.writer(writer_file, dialect=dialect, delimiter=self.delimiter)
for line in line_as_list:
assert isinstance(line,bytes)
writer.writerow(line)
Gives this error on Python3:
> writer.writerow(line)
E TypeError: a bytes-like object is required, not 'str'
But assert has no problem with the type, so why is csv
creating an error?
Can't I use BytesIO
only for both Python 2 and 3? Where is the problem here?