BERA BERA - 23 days ago 8
Python Question

Python will not work with Swedish characters

I'm trying to manipulate data stored in a table. The table columns have Swedish names with Å,Ä or Ö.

Python Shell can print the characters but when i store them in a list they wont work:

>>> print 'åäö'
åäö
>>> testlist = ['åäö', 'öööö']
>>> testlist
['\xe5\xe4\xf6', '\xf6\xf6\xf6\xf6']
>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>>>


What do i need to do to get python to work with Å,Ä and Ö?

EDIT:

This will work as suggested by @schwobaseggl:

>>> testlist = ['åäö','ööö']
>>> for x in testlist:
print x
print repr(x)
åäö
'\xe5\xe4\xf6'
ööö
'\xf6\xf6\xf6'


Im confused. What I'm really trying to do is to supply an UpdateCursor with a list of fields. I'm guessing that the field list is not looking like 'åäö' to the cursor but '\xe5\xe4\xf6' and the cursor fail when it is trying to match the fields in the list to the actual fields in the table:

with arcpy.da.UpdateCursor(fc, fieldlist) as ucursor:
for row in ucursor:
...

Answer

When printing lists, note that list's __str__ (which is implicitly called by print) uses its elements' __repr__ in order to display them. There seems to be nothing wrong. Check:

>>> testlist = ['åäö', 'öööö']
>>> print testlist
['\xc3\xa5\xc3\xa4\xc3\xb6', '\xc3\xb6\xc3\xb6\xc3\xb6\xc3\xb6']

>>> for x in testlist:
...   print x, repr(x)
...
åäö '\xc3\xa5\xc3\xa4\xc3\xb6'
öööö '\xc3\xb6\xc3\xb6\xc3\xb6\xc3\xb6'

When you are dealing with non-ascii characters, it is often better to use unicode strings. They are more well-behaved:

>>> len('öäü')
6
>>> len(u'öäü')
3
>>> s = 'äöü'
>>> s_u = s.decode('utf-8')
>>> s
'\xc3\xa4\xc3\xb6\xc3\xbc'
>>> s_u
u'\xe4\xf6\xfc'
>>> s == s_u
False