Convert unicode string in python

{u'Status': u'OK', u'City': u'Ciri\xe8', u'TimezoneName': '', u'ZipPostalCode': '', u'CountryCode': u'IT', u'Dstoffset': u'0', u'Ip': u'x.x.x.x', u'Longitude': u'7.6', u'CountryName': u'Italy', u'RegionCode': u'12', u'Latitude': u'45.2333', u'Isdst': '', u'Gmtoffset': u'0', u'RegionName': u'Piemonte'}

This is the result of my object. I would like to access the City, but it is encoded. How can I read all the parameters and decode it

>>> data['City']
u'Ciri\xe8'

>>>data['City'].decode('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 4: ordinal not in range(128)

I want the plaintext string not to be unicode. Thank!

+5
source share
3 answers

Read the following: http://nedbatchelder.com/text/unipain.html

Then just type it:

>>> data = {u'City':u'Ciri\xe8'}
>>> data['City']
u'Ciri\xe8'
>>> print data['City']
Ciriè

, Python , , Unicode u'', ASCII \xe8. print -ASCII-, Unicode . , , :

>>> print u'\xe8'
è
>>> print u'\x81'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "d:\dev\Python27\lib\encodings\cp437.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\x81' in position 0: character maps to <undefined>

437 Unicode U + 00E8, U + 0081.

+8

, , . " " , :

>>> s = u'Ciri\xe8'
>>> from unicodedata import normalize
>>> normalize('NFKD', s).encode('ASCII', 'ignore')
'Cirie'
+9

, , ascii. :

data['City'].encode('ascii','ignore')

Ciri

. : http://docs.python.org/howto/unicode.html

0

All Articles