Python window - encoding problem/special characters

2505
2
10-04-2012 01:02 AM
StefanHaglund1
Esri Contributor
Hi All,

How do we best handle special characters when using the python window?

I have this code:
>>> import arcpy
>>> mxd = arcpy.mapping.MapDocument("CURRENT")
... for lyr in arcpy.mapping.ListLayers(mxd):
...  print lyr


which prints out my layers without special characters, but when it gets to the first layer with a special character in the name I get this:
Traceback (most recent call last):
  File "<string>", line 3, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 3: ordinal not in range(128)


So, how can I get around this problem?
(Using 10.1, Build 3035)

Thanks!
Tags (2)
0 Kudos
2 Replies
BenHolmes
New Contributor
Picked an idea for this off stack overflow (link)

import arcpy,json
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
    print json.dumps(lyr.name, ensure_ascii=False)
0 Kudos
StefanHaglund1
Esri Contributor
Thank's Ben,

That was easy enough!
0 Kudos