Rename layer with unsupported signs in a mxd via python?

1012
3
Jump to solution
10-16-2014 12:43 AM
JohannesBierer
Regular Contributor

Does someone have a code example?

0 Kudos
1 Solution

Accepted Solutions
JohannesBierer
Regular Contributor

Right solution is:

lyrname.decode('utf-8')

View solution in original post

0 Kudos
3 Replies
JohannesBierer
Regular Contributor

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?

0 Kudos
JohannesBierer
Regular Contributor

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)

???

0 Kudos
JohannesBierer
Regular Contributor

Right solution is:

lyrname.decode('utf-8')

View solution in original post

0 Kudos