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)

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Feb 28, 2014 at 14:14

3 Answers 3

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")
answered Feb 28, 2014 at 14:31
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.

answered Feb 28, 2014 at 15:19
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.

answered May 4, 2016 at 10:07
1
  • 1
    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. Commented Dec 14, 2017 at 11:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.