UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 6: ordinal not in range(128)

9752
3
Jump to solution
12-02-2014 07:16 AM
TimothyHales
Esri Notable Contributor

I am trying to convert unicode to string, but I am getting UnicodeDecodeError. The original issue is related to working with a json return and creating a string, but here is a simplified example:

raw = unicode("Soporté")
print string

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 6: ordinal not in range(128)

I have tried the suggestion from here: Rename layer with unsupported signs in a mxd via python?

raw = unicode("Soporté")
string = raw.decode('utf-8')
print string

And here: Write Russian field names to txt

raw = unicode("Soporté")
string = raw.encode('utf-8')
print string

But I get the same error: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 6: ordinal not in range(128)

1 Solution

Accepted Solutions
BruceHarold
Esri Regular Contributor

raw = u'Soporté'

>>> print raw

Soporté

View solution in original post

3 Replies
BruceHarold
Esri Regular Contributor

raw = u'Soporté'

>>> print raw

Soporté

TimothyHales
Esri Notable Contributor

Thanks Bruce Harold‌! That works for printing the text, but I've added some complexity beyond printing. I am trying to concatenate the unicode with a string and append it to a list.

list = []
raw = u'Soporté'

list.append("Department: {}".format(raw))
print list
0 Kudos
TimothyHales
Esri Notable Contributor

Add the 'u' in front of my concatenated string as Bruce Harold‌ suggested worked. Thanks!

list = []  
raw = u'Soporté'  
  
list.append(u"Department: {}".format(raw))  
print list