Python-Django utf-8 csv.writer

Viewed 5163

im using python csv library for exporting my models into a csv file in django. codes are like this :

import csv
from django.http import HttpResponse

def some_view(request):
    # Create the HttpResponse object with the appropriate CSV header.
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="somefilename.csv"'

    writer = csv.writer(response)
    writer.writerow(['First row', 'Foo', 'Bar', 'Baz'])
    writer.writerow(['Second row', 'A', 'B', 'C', '"Testing"', "Here's a quote"])

    return response

My problem : in my database i have some persian characters that just works with utf-8 encoding format , so when i open generated csv file in excel the persian characters not shown correctly.

4 Answers

just add this new line code :

response.write(codecs.BOM_UTF8)

after this line :

 response['Content-Disposition'] = 'attachment; filename="somefilename.csv"'
Related