BERA BERA - 3 months ago 23
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()

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


This will work as suggested by @schwobaseggl:

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

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:


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('öäü')
>>> len(u'öäü')
>>> s = 'äöü'
>>> s_u = s.decode('utf-8')
>>> s
>>> s_u
>>> s == s_u