2

Here is my code (I am using python 2.7 )

    result = " '{0}' is unicode or something: ".format(mongdb['field'])
UnicodeEncodeError: 'ascii' codec can't encode character u'\xb0' in position 27: ordinal not in range(128)

It looks like a string I read from mongodb contains unicode. And it throws this error. How to fix it to concatenate this unicde with custom string 'is unicode or something:' ?

Thanks in advance

UPDATE

result = u" '{0}' is unicode or something: ".format(mongdb['field']) 

works for me

1
  • Is this Python 2 or Python 3? Commented Sep 13, 2013 at 22:29

2 Answers 2

9

Use a unicode format string (recommended):

result = u" '{0}' is unicode or something: ".format(mongdb['field'])

Or encode the field:

result = " '{0}' is unicode or something: ".format(mongdb['field'].encode('utf-8'))
Sign up to request clarification or add additional context in comments.

4 Comments

Second one won't work, as encoded utf-8 contains bytes outside the ASCII range.
@MarkRansom Unless I'm mistaken, Python uses ASCII by default when doing implicit encoding, but that doesn't necessarily mean the string is encoded in ASCII. You're right though, in not recommending using that method anyway; using unicode everywhere internally is definitely superior.
x = '{0}'.format(u'\u0234') gives me an ascii codec error. If I make it a Unicode literal u'{0}' it works.
@MarkRansom I know, but that's not what I'm talking about. Here, you're doing an implicit conversion, hence the error. In the answer, I was suggesting an explicit conversion: '{0}'.format(u'\u0234'.encode('utf-8')), which doesn't raise an error. Adding the u is still the recommended method though, which is why it was my first suggestion, and .encode one was the second one.
1

You have to know what encoding the text coming out of MongoDB is actually in. \xB0 suggests Windows-1252 instead of UTF-8, so try this:

 result = ("'{0}' is unicode or something"
           .format(mongdb['field'].decode('windows-1252'))

2 Comments

In Py3 it should work as written, and in Py2 I don't think you'd get that error in the first place.
Judging by the error message it's Py2, and it definitely fails there.

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.