7

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?

user31415629
  • 925
  • 6
  • 25

2 Answers2

9

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')
georgexsh
  • 15,984
  • 2
  • 37
  • 62
0

An alternate way:

import codecs

csv: StringIO

# ..

resp = response.text(f'{codecs.BOM_UTF8.decode("utf-8")}{csv.getvalue()}',
                     content_type='text/csv; charset=utf-8',
                     headers={'Content-Disposition': f'Attachment; filename={filename}'})
vladimir
  • 13,428
  • 2
  • 44
  • 70