New arcpy user needing help with changing attribute fields, and values

2342
4
01-17-2014 05:25 AM
ZacharyHassler
New Contributor
Hi

So heres my problem: I want to create a tool that will take 2 point shapefiles, Change the title of some of their attribute fields, change the value of attributes from an integer to a string (which I think I can do with a for loop with nested if/else statements), then append a third shapefile with the now modified shapefiles.

The way I'm thinking of doing this is to run feature class to feature class conversions on my 2 original shapefiles, saving the outputs in a scratch geodatabase.  I'm having trouble thinking up the code to make this happen.  Do I change the attribute field names in the conversion?  Is there another function or technique that would be better to do it afterword? How do I change the field type from a int to a string using python?  Any help would be greatly appreciated.  Thanks!
Tags (2)
0 Kudos
4 Replies
JakeSkinner
Esri Esteemed Contributor
You can change the field names using the Feature Class to Feature Class function.  Or, if you are using 10.2.1, you can use the new Alter Field function.

To change the field type, you would need to create a new field with the new type and then calculate the values from the previous field to the new one.
0 Kudos
AdamCox1
Occasional Contributor II
Hi there, I think you're on the right track with the scratch geodatabase.

Without 10.2, you can't change the name of an existing field, so you'll have to create new fields.  I would recommend these steps:

1. copy features to scratch gdb (good because you won't be editing your original data)
2. add new fields with new names and data types
3. to transfer the values from the old fields to the new, you can either use field calculate and cast the integer as a string, or you could run through the rows with cursors.  I think that if you are just going from integers to strings, field calculate should be ok (expression would be: str(!Old_Field!)).  Though, if you have null values, they will likely end up as "None".  Use cursors for more fine-grained control if you end up with special problem rows...
4. drop the old fields (this is why it's good not to alter the original data)
5. append! (merge?)

You could set this all up in model builder without too much trouble...

Of course, the problem with adding new fields is that you will not be able to retain the order of the original fields; the new ones will be on the end.  If this is a big problem, you may just want to do it manually using ArcGIS Diagrammer: http://resources.arcgis.com/gallery/file/arcobjects-net-api/details?entryID=F12ADF8F-1422-2418-34B2-...
0 Kudos
ZacharyHassler
New Contributor
Thanks for your help Adam and JSkinn

I'm still having trouble with my for loop with an if/else nested in it.  How do I reference a field in a feature class (shapefile), then append another field based on the if/else condition of the first referenced field for each iteration in the For loop going through the shapefiles features?

Thanks again for any help.
0 Kudos
AdamCox1
Occasional Contributor II
Hi, I'm not exactly clear on what you're trying to do.  My best guess is that you are using one field in shapefile A to decide whether or not to add information from shapefile B to a different field in shapefile A.  If this is so, some more details/code would be very helpful in figuring this out.

Field calculator may also work for this, because you could build a function into the calculation...
0 Kudos