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 8240273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:26:01+00:00 2026-06-07T20:26:01+00:00

This is the second time in a few weeks that I’ve been stuck on

  • 0

This is the second time in a few weeks that I’ve been stuck on an encoding issue. I’ve spent such a long time on this problem already, and I’d appreciate any help I can get.

This is what I want to do:

1) Select some rows from a MySQL table on my computer.

2) Write these rows into a text file.

3) Transfer the text file over to my Amazon EC2 Ubuntu instance.

4) Write the contents of the text file into a MySQL database.

5) Get Django to select some rows from the database in #4.

6) Show on the website.

In step #1, I just had an ordinary SELECT statement.
In step #2, I did this:

file = codecs.open('commentsfordjango.txt', encoding = 'utf-8', mode='w')
file.write(fullcomment.decode('utf8') + '\n\n\n\n\n\n')

After step #2, I opened the .txt file in Windows and I could see all the actual Chinese characters without any error.

In step #3, I just transferred the file using WinSCP.
In step #4, I did this:

file = open('/usr/local/src/blog/commentsfordjango.txt', 'r')
cursor.execute("INSERT INTO polls_poll (commenttext, pos, neu, neg) VALUES (%s, 0, 0, 0)", line)

In step #5, I did this in views.py: I simply returned the object which corresponded to the model. My model has a unicode function but I did not call that as I read that by default, it is already called when you call your object.

In step #6, my HTML file has the following line at the top of the file:

<meta charset="utf-8" />

Also, I changed my Apache encoding default to Unicode. I also made sure that my SQL database in step #4 is in Unicode.

However, after all this, my website still shows a bunch of unreadable, weird characters as such: 人在åšï¼Œå¤©åœ¨çœ‹ã€.

Any help will be very much appreciated – I’ve tried so many variations involving .decode() and .encode(‘utf-8’) and spent far too long on this problem already!

  • 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-07T20:26:03+00:00Added an answer on June 7, 2026 at 8:26 pm

    In Step #2, you should to encode your text as UTF-8.

    with open("commentsfordjango.txt", "wb") as f:
        f.write(fullcomment.encode('utf8'))
    

    In Step #3, you can then decode the data you read from the file back into unicode.

    with open("commentsfordjango.txt", "rb") as f:
        for line in f.read().decode("utf-8").splitlines():
            cursor.execute("INSERT INTO polls_poll (commenttext, pos, neu, neg) VALUES (%s, 0, 0, 0)", line)
    

    A better solution would be to just use Django’s built-in loaddata/dumpdata facilities.

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

Sidebar

Related Questions

This has been greatly bothering me in the past few weeks. In this time
For the second time I have this extremely anoying problem with an InputStream. This
I execute an ajax function that takes 3 seconds + so in this time
We're getting a very intermittent (like every few weeks someone complains about this) primary
This has gotten me completely frustrated I have a few webcombo boxes that are
I have been in this situation quite a few times where visual studio does
I've been at this for a few hours now. I'm finally giving in and
I've built a few services in Delphi 7 and did not have this problem.
I've tried working with swing (for the first time) a few weeks ago and
I am stuck with asp.net post issue with last 2 weeks. Scenario: My application

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.