Select to view content in your preferred language

Rename layer with unsupported signs in a mxd via python?

1620
3
Jump to solution
10-16-2014 12:43 AM
JohannesBierer
Frequent Contributor

Does someone have a code example?

0 Kudos
1 Solution

Accepted Solutions
JohannesBierer
Frequent Contributor

Right solution is:

lyrname.decode('utf-8')

View solution in original post

0 Kudos
3 Replies
JohannesBierer
Frequent 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
Frequent 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
Frequent Contributor

Right solution is:

lyrname.decode('utf-8')

0 Kudos