{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'}
这是我对象的输出。我想进入城市,但它是编码的。如何读取所有参数并解码
>>> 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)
我想要纯文本而不是unicode字符串。谢谢您!
你想要什么还不清楚。如果“纯文本”是指删除重音符号,请尝试以下操作:
读这个:http://nedbatchelder.com/text/unipain.html
然后打印出来:
如果不打印,Python将打印字符串的安全表示形式,表示它是Unicode文本
u''
,并且包含非ASCII字符\xe8
。print
试图通过在终端编码中对Unicode字符串进行编码来显示非ASCII字符。如果字符串包含终端编码不支持的字符,则可能失败:在上面的示例中,code page 437支持Unicode字符U+00E8,但不支持U+0081。
纯文本,我想你是说ascii。为此,您可以使用:
这将删除unicode字符并返回
有关详细信息,请参阅此链接:http://docs.python.org/howto/unicode.html
相关问题 更多 >
编程相关推荐