Select to view content in your preferred language

Str.format() function fails on Norwegian characters

472
2
10-24-2013 03:19 AM
JosteinSvegården
Deactivated User
I have a script with a SearchCursor that runs through a feature layer and writes the feature properties to a semicolon divided text file. When it comes to a feature which has a property that contains one of the Norwegian letters æ, ø or å (html: æ, ø or å), the script crashes on the str.format() function

Traceback info:
  File "C:\Infrastructure_calculation.py", line 97, in <module>
    propStr += ";{0}".format(IS[index])

Error Info:
'ascii' codec can't encode character u'\xc6' in position 1: ordinal not in range(128)

ISs = da.SearchCursor("ISpoint_lyr", "*")
for IS in ISs:
    for index in range(len(ISs.fields)):
        if index <> shapeFieldIndex:
             propStr += ";{0}".format(IS[index])


I guess this have something to do with unicode encoding, but I have not been able to find a solution. Any input is appreciated!
Tags (2)
0 Kudos
2 Replies
JasonScheirer
Esri Alum
Mark the format string as unicode (with a u beforehand u"like this") so it doesn't try to downcast your Norwegian characters to ascii.

ISs = da.SearchCursor("ISpoint_lyr", "*")
for IS in ISs:
    propStr = u""
    for index in range(len(ISs.fields)):
        if index <> shapeFieldIndex:
             propStr += u";{0}".format(IS[index])
0 Kudos
JosteinSvegården
Deactivated User
Thanks, Jason, that did it!

It is easy to get lost in all this encoding stuff. So for readers that might bump into this thread later, I will add that to be able to write the resulting string to a file, I had to convert the unicode string to a string of bytes using the string.encode() function:

STR_result = STR_result.encode('utf8', 'replace')


See http://pythonhosted.org/kitchen/unicode-frustrations.html
0 Kudos