How to decode a unicode string Python [duplicate]

我的梦境 提交于 2019-12-07 00:57:17

问题


What is the best way to decode an encoded string that looks like: u'u\xf1somestring' ?

Background: I have a list that contains random values (strings and integers), I'm trying to convert every item in the list to a string then process each of them.

Turns out some of the items are of the format: u'u\xf1somestring' When I tried converting to a string, I get the error: UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in position 1: ordinal not in range(128)

I have tried

item = u'u\xf1somestring'
decoded_value = item.decode('utf-8', 'ignore')

However, I keep getting the same error.

I have read up about unicode characters and tried a number of suggestions from SO but none have worked so far. Am I missing something here?


回答1:


You need to call encode function and not decode function, as item is already decoded.

Like this:

decoded_value = item.encode('utf-8')



回答2:


That string already is decoded (it's a Unicode object). You need to encode it if you want to store it in a file (or send it to a dumb terminal etc.).

Generally, when working with Unicode, you should (in Python 2) decode all your strings early in the workflow (which you already seem to have done; many libraries that handle internet traffic will already do that for you), then do all your work on Unicode objects, and then at the very end, when writing them back, encode them to whatever encoding you're using.



来源:https://stackoverflow.com/questions/35083374/how-to-decode-a-unicode-string-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!