3

I need to update datasource of Picture element of layout element of a mapdocument. but it is showing Unicode decode error. mxd= arcpy.mapping.MapDocument("CURRENT") for elm in arcpy.mapping.ListLayoutElements(mxd,"PICTURE_ELEMENT"): oldname = elm.name ... if elm.name==oldname: ... elm.name=oldname.replace(r"ä","ae") ... print elm.name

after running the code I get following error message:

Runtime error Traceback (most recent call last): File "", line 4, in UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)

2

I'm not an expert when it comes to dealing with unicode characters, but it looks like you need to do the replace using the unicode representation of the character "ä":

elm.name=oldname.replace(u"\xe4","ae")
2

I myself deal quite often with Unicode characters due to having different languages used for attributes storage within GIS datasets I handle. One of the techniques I've learnt regarding dealing with Unicode is that sometimes it is helpful to replace Unicode characters to the closest Latin one. This is particularly useful when you have two data sources and in one you have "Café" and in another "Cafe". This works fine for most diacritic symbols I've worked with, too.

You can for sure run replace on strings, but in case you need to automate this process and you don't want to end up creating a dictionary where keys=source symbols (ä) and values=the target symbols (ae), you can use this function.

import unicodedata

def strip_accents(s):
   return ''.join(c for c in unicodedata.normalize('NFD', s)
                  if unicodedata.category(c) != 'Mn')

str_input = u'\xe4' #your input string you get
print strip_accents(str_input)

More help information on Unicode in Python.

1

I prefer to use utf-8 throughout the script, encoding any strings where neccessary.

oldname = elm.name.encode('utf-8')
...
elm.name=oldname.replace(r"ä".encode('utf-8'),"ae".encode('utf-8'))

It is also useful to declare the encoding at the top of each script.

# -*- coding: utf-8 -*-

See Alex's link to the Python Unicode page for more information.

  • Since posting this, I've also learned that python 3 strings are all unicode - there is no more str. You can utilise this in python 2 by adding from __future__ import print_function, unicode_literals above your imports. – jon_two Dec 14 '17 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.