0

I'm trying to export a csv file with utf-8 but csv.writer doesn't seems to have an encoding parameter .... any idea ?

import csv

response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=data.csv'
writer = csv.writer(response, delimiter=';')

for book in books:
   writer.writerow([book.author, book.title])

The string journée appears as journ@~e

Panagiotis Kanavos
  • 90,087
  • 9
  • 138
  • 171
MichaelS
  • 121
  • 7
  • 1
    [The docs](https://docs.python.org/3/library/csv.html) show (and explain) that since `csv` reads from a file, the encoding needs to be specified when opening that file: `specify the encoding argument when opening the output file.`. In this case the "file" is the HttpResponse. Have you tried specifying the `charset` in the response? – Panagiotis Kanavos Apr 07 '21 at 13:28
  • yes, charset='utf-8' doesnt work – MichaelS Apr 07 '21 at 13:31
  • What does `doesn't work` mean? You get garbled text? Or something else? The *cliient* needs the charset to know how to handle the response, so you *must* include it anyway – Panagiotis Kanavos Apr 07 '21 at 13:32
  • yes I have something like journ@~e instead of journée – MichaelS Apr 07 '21 at 13:33
  • Where? In the browser or when the downloaded file is opened in an editor? I'd expect that to happen if the `charset` is missing, as the browser would try to handle the two bytes needed to represent `é` as a single-byte text. So the output *is* UTF8, but since the proper header is missing, the text is treated as single-byte text – Panagiotis Kanavos Apr 07 '21 at 13:36
  • Try [pandas](https://pandas.pydata.org/). Then you can [specify encoding](https://stackoverflow.com/a/16923367/3439404) for output file. – JosefZ Apr 07 '21 at 13:39
  • @JosefZ that won't specify the HTTP response's charset. The file is already UTF8 after all. The problem is the missing charset – Panagiotis Kanavos Apr 07 '21 at 13:41
  • @MichaelS are you using Django? Is that a Django HttpResponse? How did you set the charset? Through the content type? The `charset` attribute? How are you *reading* the file? – Panagiotis Kanavos Apr 07 '21 at 13:55
  • Does this answer your question? [django HttpResponse and unicode](https://stackoverflow.com/questions/17128799/django-httpresponse-and-unicode) – Panagiotis Kanavos Apr 07 '21 at 14:02
  • Yes it's django HttpResponse, the content is in UTF-8 in my database. The problem is when I open the csv file with excel. Thanks :) – MichaelS Apr 07 '21 at 14:18
  • I tried to add charset in : response = HttpResponse(content_type='text/csv', charset='utf-8') – MichaelS Apr 07 '21 at 14:20
  • If you save the file to disk, then open it with Excel, the `charset` parameter is no longer relevant. Then it's simply Excel failing to interpret UTF-8 correctly. If I am not mistaken, Excel will correctly decode CSV files in (non-standard) UTF-8-BOM or UTF-16. If Excel shows an import dialog during opening, you also may get a chance to tell it the correct encoding. – lenz Apr 07 '21 at 19:46

0 Answers0