5

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 4

19

i try encode('UTF-8') Solution in some cases, but the result in microsoft excel is not readable , it shows b'\xd8\xb1\xdb\x8c\xd8'. However i find the correct way for showing CSV in excel readable.

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"'

    response.write(u'\ufeff'.encode('utf8'))
    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

Just add response.write(u'\ufeff'.encode('utf8')) into the code

2
  • i quess u should try encode('utf8') at writer.writerrow not in response.write
    – Abi Waqas
    Jul 4, 2017 at 6:59
  • so it doesn't ask user to encoding ? while opening? Feb 4, 2021 at 11:19
5

just add this new line code :

response.write(codecs.BOM_UTF8)

after this line :

 response['Content-Disposition'] = 'attachment; filename="somefilename.csv"'
1
  • This was what worked for me! Simply adding this excel will decode Chinese characters correctly. Thanks! Nov 10, 2021 at 6:50
0

use this i have arabic character and worked fine for me

def university_csv(request):
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="university_list.csv"'
    writer = csv.writer(response)
    university_obj = Universities.objects.using('cms').all()
    writer.writerow(['id', 'NAME', 'ABBREVIATION', 'ADDRESS'])
    for item in university_obj:
        if item.address:
            if item.abbreviation:
                writer.writerow([item.id, item.name.encode('UTF-8'), item.abbreviation.encode('UTF-8'),
                                 item.address.encode('UTF-8')])
            else:
                writer.writerow([item.id, item.name.encode('UTF-8'), item.abbreviation, item.address.encode('UTF-8')])
        else:
            if item.abbreviation:
                writer.writerow([item.id, item.name.encode('UTF-8'), item.abbreviation.encode('UTF-8')])
            else:
                writer.writerow([item.id, item.name.encode('UTF-8')])

    return response
4
  • u can see the format for utf and ignore the if else condition.
    – Abi Waqas
    Jul 3, 2017 at 6:54
  • i try this before but the result is not a readable string its utf-8 literal like \xd8\x80 or some thing like that.
    – Arash
    Jul 4, 2017 at 5:47
  • what you mean ?
    – Arash
    Jul 4, 2017 at 6:03
  • u must have written utf code somewhere can u please write your updated code
    – Abi Waqas
    Jul 4, 2017 at 6:07
-1

First of all add a:

# coding: utf-8

At the top of your .py file. Second, it's not an Django issue, it's rather the MS Excel issue. I had a similar problem last week and found that in excel when you choose the encoding you just need to set the language code (or something like that) to UTF-8 family rather the Persian.

2
  • If you open the .csv file in a normal notepad and you see your letters then everything is ok'ey with the file. Jul 3, 2017 at 6:53
  • If something is wrong with the letters then add the .encode('UTF-8') on the fields that have your letters. Jul 3, 2017 at 6:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.