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

Sign up to request clarification or add additional context in comments.

2 Comments

i quess u should try encode('utf8') at writer.writerrow not in response.write
so it doesn't ask user to encoding ? while opening?
5

just add this new line code :

response.write(codecs.BOM_UTF8)

after this line :

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

1 Comment

This was what worked for me! Simply adding this excel will decode Chinese characters correctly. Thanks!
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 Comments

u can see the format for utf and ignore the if else condition.
i try this before but the result is not a readable string its utf-8 literal like \xd8\x80 or some thing like that.
what you mean ?
u must have written utf code somewhere can u please write your updated code
-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 Comments

If you open the .csv file in a normal notepad and you see your letters then everything is ok'ey with the file.
If something is wrong with the letters then add the .encode('UTF-8') on the fields that have your letters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.