Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8773057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:11:50+00:00 2026-06-13T18:11:50+00:00

I have created the csv file through django. I have write the encoded data

  • 0

I have created the csv file through django. I have write the encoded data into that. but when i open this file in excel sheet then the unicode characters not display properly.

I also refer this question
Django create CSV file that contains Unicode and can be opened directly with Excel

But didn’t get the proper answer. I have tried all the answers, but didn’t work any of them.

I write the code as below.

    def exportcsv(request):
        import csv
        producer_list = Producer.objects.filter()
        response = HttpResponse(mimetype='text/csv')
        response['Content-Disposition'] = 'attachment; filename=producer.csv'
        writer = csv.writer(response, delimiter=",")
        writer.writerow(codecs.BOM_UTF16_LE)
        writer.writerow(['Produsenter','Type','Land','Region'])
        for cdst in producer_list:
            writer.writerow([cdst.title.encode("UTF-8"),
                            cdst.producer_type.encode("UTF-8"),
                            cdst.country.country.encode("UTF-8"),
                            cdst.region.region.encode("UTF-8")])
        return response

Then the csv file created properly, but the characters in that not encoded properly.
The character will be display like “Tokaj Hétszölö”.

When i try

writer.writerow([cdst.title.encode("iso-8859-1"),
                            cdst.producer_type.encode("iso-8859-1"),
                            cdst.country.country.encode("iso-8859-1"),
                            cdst.region.region.encode("iso-8859-1")])

Then the data will we added properly also open it excel file properly.
But it give error for some characters like ‘æ’ and ‘ in the string.

Error: ‘latin-1′ codec can’t encode character u’\u2013’ in position 266: ordinal not in range(256)

I also try the below code.

response['Content-Disposition'] = 'attachment; filename=producer.csv'
response.write(u'\ufeff'.encode('utf8'))
writer = csv.writer(response, delimiter=",")

Also try

writer.writerow(codecs.BOM_UTF16_LE)
writer.writerow(str.decode('utf8').encode('utf_16_le'))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:11:51+00:00Added an answer on June 13, 2026 at 6:11 pm

    I have resolve above issue. I write the code as below.

    writer.writerow([cdst.title.encode("iso-8859-1"),
                     cdst.producer_type.encode("iso-8859-1"),
                     cdst.country.country.encode("iso-8859-1"),
                     cdst.region.region.encode("iso-8859-1")])
    

    using above code i got the error as

    Error: 'latin-1' codec can't encode character u'\u2013' in position 266: ordinal not in range(256)
    

    But this error is because of empty string and for ‘-‘ character when i pass to encoding. This can be resolved when i set condition before passing the string to encoding and replace the ‘-‘ character with ‘_’.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Python script that reads through a text csv file and creates
I have an ANSI-encoded CSV file that contains a number of 'problem' special characters.
I have a stored procedure called sp_BulkInsert that inserts one .csv file into my
I have an exe that already creates a csv file. If I save the
I have a PHP app that creates a CSV file which is forced to
I have a .csv file containing 3 columns of data. I need to create
I have a data structure created using read.csv() of dimensions 130, 395. I am
I am coding a survey that outputs a .csv file. Within this csv I
I have created a script that analyzes a set of raw data and converts
I have a VB web application that reads from a CSV file which contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.