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!
Solved! Go to Solution.
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.
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.
|
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.
Thank you Dan that was part of the problem
Natalia Gutierrez With that part resolved... what now remains?
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?
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...
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.
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.
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.
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