Using Python Dictionaries to Change Feature Class Names

2017
11
Jump to solution
04-30-2020 08:48 AM
NataliaGutierrez1
New Contributor III

Hello!

I recently posted a question about python dictionaries and here I am again struggling with them..

I am now trying to use them to change feature class names.

Problem:

I have a list of 77 feature classes and I need to change their names from codes to names using a python dictionary.

For example:

Feature class name: T14_single_points

New name that I need: Alachua

I am not getting any errors when running this code but I am not getting any results either.

import arcpy

# script to change fc name using a dictionary

arcpy.env.workspace = r"D:\APRX_MXDS\USA_Parcels_2019_Project\test.gdb"
arcpy.env.overwriteOutput = True

fc_list = arcpy.ListFeatureClasses("points*") # list of fc that end with points

# dictionary
county_codes = {14 : "Bradford", 15 : "Brevard", 16 : "Broward"}

for fc in fc_list: # loop through the feature classes
    key = "{[1:3]}".format(fc.name) # let the second and third characters in the fc name be the key
    if key in county_codes: 
        fc = county_codes.get(key) # assign the value in the dictionary as the new feature class name‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

thank you!

0 Kudos
1 Solution

Accepted Solutions
JoeBorgione
MVP Emeritus

I thought that was what you might be thinking.  Take a step back and study your code for a minute, and ask yourself, what exactly is fc?

It's just a variable and the value of that variable changes as the for loop progresses.  Run the snippet of code:

for fc in fc_list: 
    key = "{[1:3]}".format(fc.name) 
    if key in county_codes:
        print(fc) 
        fc = county_codes.get(key)
        print(fc)
       

And when it's done, take a look at your geodatabase and see if names have changed.

In order to change the name of the actual feature class, you're going to do a little more work. Take a look Rename—Data Management toolbox | Documentation and see where that takes you.

That should just about do it....

View solution in original post

0 Kudos
11 Replies
DanPatterson_Retired
MVP Emeritus
if len(fc_list) == 0:
    print("something is wrong")‍‍
else:
    print("processing stuff")‍‍‍‍"points*") # list of fc that end with points

could be the issue

wild_card

Limits the results returned. If a value is not specified, all values are returned. The wildcard is not case sensitive.

SymbolDescriptionExample

*

Represents zero or more characters.

Te* finds Tennessee and Texas.

did you mean

list of fc that begins with points

or did you mean

"*points") # list of fc that end with points

in any event that might account for one instance where nothing is processed.

NataliaGutierrez1
New Contributor III

Thank you Dan that was part of the problem  

0 Kudos
DanPatterson_Retired
MVP Emeritus

Natalia Gutierrez  With that part resolved... what now remains?

0 Kudos
JoeBorgione
MVP Emeritus

Hello Natalia Gutierrez-  to add to Dan Patterson's comments; I don't see you renaming anything, or is that the next stage in your script?

That should just about do it....
0 Kudos
NataliaGutierrez1
New Contributor III

Hi Joe, I thought I was renaming in the last line of my script. Line # 15. I guess I am really lost with this one...

0 Kudos
JoeBorgione
MVP Emeritus

I thought that was what you might be thinking.  Take a step back and study your code for a minute, and ask yourself, what exactly is fc?

It's just a variable and the value of that variable changes as the for loop progresses.  Run the snippet of code:

for fc in fc_list: 
    key = "{[1:3]}".format(fc.name) 
    if key in county_codes:
        print(fc) 
        fc = county_codes.get(key)
        print(fc)
       

And when it's done, take a look at your geodatabase and see if names have changed.

In order to change the name of the actual feature class, you're going to do a little more work. Take a look Rename—Data Management toolbox | Documentation and see where that takes you.

That should just about do it....
0 Kudos
NataliaGutierrez1
New Contributor III

Thank you Joe. As I told Dan, your answers will guide me for the next time I have to work on something similar. In the end I had to do it manually because it was taking me too long to automate it. 
 

0 Kudos
JoeBorgione
MVP Emeritus

Sorry it didn't work out for you; automation can sometimes be a bump in the road.  Like anything else though, the more you use it, the better you get at it.

That should just about do it....
0 Kudos
NataliaGutierrez1
New Contributor III

yes definitely getting a lot better! thanks to you guys in here.

I really appreciate it! I don't know what I would do without your help 

0 Kudos