I'm using python 3 trying to generate a csv on the file. I want to ensure that I'm writing utf8 so I'm converting values of my list of dicts into byte strings
field_order = ['field1', 'field2', 'field3', 'field4']
stats = ... # list of dicts
output = io.BytesIO()
writer = csv.DictWriter(output, field_order)
writer.writeheader()
for stats in my_stats:
writer.writerow({k: bytes(v, 'utf8') for k, v in stats.items()})
csv_output = output.getvalue()
I'm getting an exception on the writer.writeheader() call
TypeError: 'str' does not support the buffer interface
There doesn't seem to any way to change writerheader to write bytes. What am I doing wrong?
csv
mdoule operates on strings according to the documentation:
Writer objects (DictWriter instances and objects returned by the writer() function) have the following public methods. A row must be a sequence of strings or numbers for Writer objects and a dictionary mapping fieldnames to strings or numbers (by passing them through str() first) for DictWriter objects. Note that complex numbers are written out surrounded by parens. This may cause some problems for other programs which read CSV files (assuming they support complex numbers at all).
How about using io.StringIO
, and later encode it utf-8
.
import csv
import io
field_order = ['field1', 'field2', 'field3', 'field4']
my_stats = ...
output = io.StringIO()
writer = csv.DictWriter(output, field_order)
writer.writeheader()
for stats in my_stats:
writer.writerow(stats)
csv_output = output.getvalue().encode('utf-8')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With