How to decode a unicode string Python

mfalade picture mfalade · Jan 29, 2016 · Viewed 41.3k times · Source

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?

Answer

Sameer Mirji picture Sameer Mirji · Jan 29, 2016

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

Like this:

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