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?
You need to call encode
function and not decode
function, as item
is already decoded.
Like this:
decoded_value = item.encode('utf-8')
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With