Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 DictWriter csv BytesIO TypeError

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?

like image 658
Andrew Kou Avatar asked Jan 10 '23 03:01

Andrew Kou


1 Answers

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')
like image 67
falsetru Avatar answered Jan 22 '23 11:01

falsetru