Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I need to update datasource of Picture element of laout 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)

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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")
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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