I'm trying to generate a CSV that opens correctly in Excel, but using StringIO instead of a file.
output = StringIO("\xef\xbb\xbf") # Tried just writing a BOM here, didn't work
fieldnames = ['id', 'value']
writer = csv.DictWriter(output, fieldnames, dialect='excel')
writer.writeheader()
for d in Data.objects.all():
writer.writerow({
'id': d.id,
'value': d.value
})
response = HttpResponse(output.getvalue(), content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=data.csv')
return response
This is part of a Django view, so I'd really rather not get into the business of dumping out temporary files for this.
I've also tried the following:
response = HttpResponse(output.getvalue().encode('utf-8').decode('utf-8-sig'), content_type='text/csv')
with no luck
What can I do to get the output file correctly encoded in utf-8-sig
, with a BOM, so that Excel will open the file and show multi-byte unicode characters correctly?