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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:55:40+00:00 2026-06-16T06:55:40+00:00

I have a function like this: def convert_to_unicode(data): row = {} if data ==

  • 0

I have a function like this:

def convert_to_unicode(data):
    row = {}
    if data == None:
        return data
    try:
        for key, val in data.items():
            if isinstance(val, str):
                row[key] = unicode(val.decode('utf8'))
            else:
                row[key] = val
        return row
    except Exception, ex:
        log.debug(ex)

to which I feed a result set (got using MySQLdb.cursors.DictCursor) row by row to transform all the string values to unicode (example {'column_1':'XXX'} becomes {'column_1':u'XXX'}).

Problem is one of the rows has a value like {'column_1':'Gabriel García Márquez'}
and it does not get transformed. it throws this error:

'utf8' codec can't decode byte 0xed in position 12: invalid continuation byte

When I searched for this it seems that this has to do with ascii encoding.

The solutions i tried are:

  1. adding # -*- coding: utf-8 -*- at the beginning of my file … does not help

  2. changing the line row[key] = unicode(val.decode('utf8')) to row[key] = unicode(val.decode('utf8', 'ignore')) … as expected it ignores the non-ascii character and returns {'column_1':u'Gabriel Garca Mrquez'}

  3. changing the line row[key] = unicode(val.decode('utf8')) to row[key] = unicode(val.decode('latin-1')) … Does the job but I am afraid it will support only West Europe characters (as per Here )

Can anybody point me towards a right direction please.

  • 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-16T06:55:41+00:00Added an answer on June 16, 2026 at 6:55 am

    Firstly:

    • The data you’re getting in your result set is clearly latin-1 encoded, or you wouldn’t be observing this behavior. It is entirely correct that trying to decode a latin-1-encoded byte string as though it were utf-8-encoded blows up in your face. Once you have a latin-1-encoded byte string foo, if you want to convert it to the unicode type, foo.decode('latin1') is the right thing to do.

    • I noticed the expression unicode(val.decode('utf8')) in your code. This is equivalent to just val.decode('utf8'); calling the .decode method of a byte string converts it to unicode, so you’re calling unicode() on a unicode string, which just returns the unicode string.

    Secondly:

    • Your real problem here – if you want to be able to deal with characters not included in the character set supported by the latin-1 encoding – is not with Python’s string types, per se, so much as it is with the MySQLdb library. I don’t know this problem in intimate detail, but as I understand it, in ancient versions of MySQL, the default encoding used by MySQL databases was latin-1, but now it is utf-8 (and has been for many years). The MySQLdb library, however, still by default establishes latin-1-encoded connections with the database. There are literally dozens of StackOverflow questions relating to MySQL, Python, and string encoding, and while I don’t fully understand them, one easy-to-use solution to all such problems that seems to work for people is this one:
      http://www.dasprids.de/blog/2007/12/17/python-mysqldb-and-utf-8

    I wish I could give you a more comprehensive and confident answer on the MySQLdb issue, but I’ve never even used MySQL and I don’t want to risk posting anything untrue. Perhaps someone can come along and provide more detail. Nonetheless, I hope this helps you.

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

Sidebar

Related Questions

I have a function friend_exists like this: def friend_exists(request, pid): result = False try:
I have a function like this: def eventcateg_detail(request): ca = EventTypeCategory.objects.values() for i in
Suppose I have a function like this (I use akka 2.0.2): def foo(message: String):
I have a function like this function login_redirect($redirect, $vars = array()) { return $redirect;
Suppose I have a function like this: def getNeighbors(vertex) which returns a list of
I have a function like this: private def add[T](n: String, t: T, k: Map[String,T]):
Say I have a function like this: user=> (def m {10 5, 5 2,
I have a function like this: def foo(bar): ... Now bar can either be
I have function like this in python: def test_ssh(host, username, password): ssh = paramiko.SSHClient()
I have function like this: function ypg_delete_img($id, $img) { $q = $this->ypg_get_one($id); $imgs =

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.