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
2
  • 5
    Remove [0]. You are slicing 'map num:' and only keeping first char which is m
    – Bera
    Commented Jun 24, 2019 at 15:49
  • 2
    BERA, you right. write it as your answer please.
    – newGIS
    Commented Jun 25, 2019 at 4:55

1 Answer 1

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 and acknowledge you have read our privacy policy.

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