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

  • SEARCH
  • Home
  • 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 8681985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:39:04+00:00 2026-06-12T21:39:04+00:00

I have a Python 2 Pyramid web app using SQLAlchemy to talk to a

  • 0

I have a Python 2 Pyramid web app using SQLAlchemy to talk to a MySQL table, of which all string columns are UTF-8 encoded. When I pull the data to display, I must use .decode("UTF-8") in order for it to show, otherwise I get the natural error of ASCII can not decode.

I have two questions:

  1. Is there any other way of working to avoid the need of .decode("UTF-8") each and every time?

  2. If I want to push something into the database, and I have a string which is s = u'str', do I need to do anything to it when it’s to be insterted to a UTF-8 column?

Thank you very much.

For people who might find this message through a google search:
If you encounter an error, sort of:

UnicodeDecodeError: ‘ascii’ codec can’t decode byte in

Do use .encode(..)

  • 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-12T21:39:05+00:00Added an answer on June 12, 2026 at 9:39 pm

    If your SQLAlchemy columns are of the Unicode type instead of String, SQLAlchemy will do the character encoding/decoding (in your case to/from UTF-8) for you.

    Note that the String column type has a convert_unicode parameter which can be set to True, but this should only be used for the very rare cases where the database backend doesn’t have native Unicode support.

    As @MartijnPieters mentioned on his comment, you should be aware of the MySQL Unicode section in the SQLAlchemy documentation. Namely, if you don’t explicitly set the character encoding in the connection to the database with:

    # set client encoding to utf8; all strings come back as unicode
    create_engine('mysql+mysqldb:///mydb?charset=utf8')
    

    (the following is mostly quoted from the SQLAlchemy documentation)

    “[…] many MySQL server installations default to a latin1 encoding for client connections, which has the effect of all data being converted into latin1, even if you have utf8 or another character set configured on your tables and columns. The charset parameter as received by MySQL-Python also has the side-effect of enabling use_unicode=1.”

    “Manually configuring use_unicode=0 will cause MySQL-python to return encoded strings:”

    # set client encoding to utf8; all strings come back as utf8 str
    create_engine('mysql+mysqldb:///mydb?charset=utf8&use_unicode=0')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have deployed a Pyramid app using mod_wsgi. I have setup the python path
I am starting a web project in Python, most likely using Django or Pyramid.
I've just started learning Python recently and am using Pyramid as my web framework.
I have python script which downloads N number of images from website. I run
I have Python classes, of which I need only one instance at runtime, so
I have Python script bgservice.py and I want it to run all the time,
I'm creating a python pyramid app on Heroku, and I was wondering if anyone
I have python/django app on Heroku (Cedar stack) and would like to make it
I have Python 2.7 installed on OpenSUSE. I'm using the pty module to spawn
I have a Pyramid application which uses request.environ['REMOTE_ADDR'] in some places. The application is

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.