Does someone have a code example?
Solved! Go to Solution.
This:
import arcpy
import os
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.supports("DATASOURCE"):
arcpy.AddMessage("Layer: " + lyr.name + " Source: " + lyr.dataSource)
for lyr in arcpy.mapping.ListLayers(mxd):
lyrname = str(lyr.name)
print lyrname
lyrname_replaced = lyrname.replace(" ","_")
lyr.name = lyrname_replaced
arcpy.AddMessage(lyrname_replaced)
arcpy.RefreshTOC()
leads to this error:
File "R:\Karto\zGIS\Python\RenameLayerinMxd.py", line 16, in <module>
lyrname = str(lyr.name)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 3: ordinal not in range(128)
Any ideas?
If I use
lyrname.encode('utf-8')
This leads to:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 3: ordinal not in range(128)
???
Right solution is:
lyrname.decode('utf-8')