Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python string unicode issue

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

like image 386
wwli Avatar asked Sep 22 '26 09:09

wwli


1 Answers

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'))
like image 54
Thomas Orozco Avatar answered Sep 24 '26 21:09

Thomas Orozco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!