While migrating to Python 3, I noticed some files we generate using the built-in csv
now have b'
prefix around each strings...
Here's the code, that should generate a .csv for a list of dogs
, according to some parameters defined by export_fields
(thus always returns unicode data):
file_content = StringIO()
csv_writer = csv.writer(
file_content, delimiter='\t', quotechar='"', quoting=csv.QUOTE_MINIMAL
)
csv_writer.writerow([
header_name.encode('cp1252') for _v, header_name in export_fields
])
# Write content
for dog in dogs:
csv_writer.writerow([
get_value(dog).encode('cp1252') for get_value, _header in export_fields
])
The problem is once I returns file_content.getvalue()
, I get:
b'Does he bark?' b'Full Name' b'Gender'
b'Sometimes, yes' b'Woofy the dog' b'Male'
Instead of (indentation has been modified to be readable on SO):
'Does he bark?' 'Full Name' 'Gender'
'Sometimes, yes' 'Woofy the dog' 'Male'
I did not find any encoding
parameter in the csv
module. I would like the whole file to be encoded in cp1252, so I don't really care either the encoding is done through the iteration of the lines or on the file construted itself.
So, does anyone know how to generate a proper string, containing only cp1252 encoded strings?