Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StringIO generated csv file that includes BOM

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?

like image 834
user31415629 Avatar asked Oct 09 '17 14:10

user31415629


1 Answers

HttpResponse accepts bytes:

output = StringIO()
...
response = HttpResponse(output.getvalue().encode('utf-8-sig'), content_type='text/csv')

or let Django do encoding:

response = HttpResponse(output.getvalue(), content_type='text/csv; charset=utf-8-sig')
like image 141
georgexsh Avatar answered Oct 29 '22 11:10

georgexsh