2

I try to replace part of text in 23 mxd files from "d" into 'map num: '. The code run without errors, but the result in the maps is only the character "m" instead of 'map num: ' I run this code:

import arcpy
from arcpy import env

env.workspace = r"G:\PROJECTS\road70-75"
counter = 0
for mxdname in arcpy.ListFiles("*.mxd"):
    print mxdname
    mxd = arcpy.mapping.MapDocument(
    r"G:\PROJECTS\road70-75\\" + mxdname)
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
        if elm.text.startswith(u'd'): 
            elm.text = elm.text.replace(u'd', u'map num: ')[0]
            print 'elm.text','\n'
            counter = counter + 1
    mxd.save()
del mxd
print 'total maps: ', counter
  • 5
    Remove [0]. You are slicing 'map num:' and only keeping first char which is m – BERA Jun 24 at 15:49
  • 2
    BERA, you right. write it as your answer please. – newGIS Jun 25 at 4:55
2

Remove [0] from elm.text = elm.text.replace(u'd', u'map num: ')[0]. You are slicing 'map num:' and only keeping first char (index 0) which is m

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.