Read a MXD file and change all comma and blank spaces to nothing with Python, how to?

1093
7
08-22-2011 04:16 AM
OskarKarlsson2
New Contributor
I am going to upgrade a system from ArcIMS to ArcGIS Server and the MXD documents that ArcIMS used contained commas and blanks pace.
In ArcGIS Server they can not contain commas or blank spaces. I will get a error message if I use it.

Is there someone that has a Python Script that reads a MXD file and changes the comma (,) and blank spaces to nothing?

Best Regards
Oskar
Tags (2)
0 Kudos
7 Replies
BruceNielsen
Occasional Contributor III
This may not be the most efficient or elegant, but it works:
newName=''.join(''.join(oldName.split()).split(',')) #All of the quotes are single quotes
0 Kudos
OskarKarlsson2
New Contributor
But the other thing is that I have about a hounded MXD documents that I want to change the layer names within the Map document. Is there a script that will do this automatically?

Thanks for your response!
0 Kudos
BruceNielsen
Occasional Contributor III
Assuming that you're using ArcGIS 10, you will have to write a script that loops through all of the MXDs. Within that loop is another loop that works through all of the layers, and applies the changes.

I'm stuck at 9.2, so I can't help you on that part. I believe that some other posts in this forum may give you some clues on how to accomplish it.

Good Luck,
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Here is an example on how to do this:

import os
workspace = r"C:\temp\python"

for (path, dirs, files) in os.walk(workspace):
    for file in files:
        if ".mxd" in file.lower():
            if "," in file:
                str = os.path.join(path, file)
                new = str.replace(",", "")
                os.rename(str, new)
            elif " " in file:
                str = os.path.join(path, file)
                new = str.replace(" ", "")
                os.rename(str, new)


You will just need to update 'workspace = r"C:\temp\python"' with the directory that contains the MXDs.  The example above will also loop through all sub directories.
0 Kudos
OskarKarlsson2
New Contributor
Thanks Jake S but the intention of the script is not to change the name of the MXD file but to change the name of each layer within the MXD file.

But the script works great to change the name of the MXD. 😃
I think I am going to learn to script in Python it seem like a great language to program in.

I simply want to change the layers namne Conturs, height to Contursheight inside of a MXD document.

Is there some way to do this?

Grateful for the help!

// Oskar
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Sorry for the misunderstanding.  Here is an example on how to change the layer names:

import arcpy, os
from arcpy import env
from arcpy import mapping
env.workspace = r"C:\temp\python"

env.overwriteOutput = True

for (path, dirs, files) in os.walk(env.workspace):
    for file in files:
        if ".mxd" in file.lower():
            mxd = os.path.join(path, file)
            mxd = mapping.MapDocument(mxd)
            for df in mapping.ListDataFrames(mxd, "*"):
                for lyr in mapping.ListLayers(mxd, "*", df):
                    if " " in lyr.name:
                        lyr.name = lyr.name.replace(" ", "")
                for lyr in mapping.ListLayers(mxd, "*", df):
                    if "," in lyr.name:
                        lyr.name = lyr.name.replace(",", "")
            mxd.save()
            
del mxd
0 Kudos
OskarKarlsson2
New Contributor
Thanks works absolutely perfect!

You have inspired me to program for myself in Python and have started to do so now.

Thanks a lot!
0 Kudos