Add Field then Set the Field Value?

1200
12
Jump to solution
07-30-2012 11:20 AM
RobertStewart
New Contributor II
I have a python script in which the end result is a single polygon in a file geodatabase that is added to the current map view. That part works fine.

What I want to do is create 2 new fields and set the values in those fields to be two parameters set in the original run of the tool (getparametersastext).

For example, would like to create two new fields: ZFile and Planner
Then set those fields to be equal to the parameters set manually at the start of the tool: Zfile and Planner

I've successfully created the fields, but I've had no success at setting the values for either.

As I mentioned, there is only one row in the dataset. I can set it manually, but the idea is to automate all of this.

Any points in the right direction are gratefully accepted.

Cheers!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ChrisSnyder
Regular Contributor III
Yes, it'd be good to change the field types to text...

How about this:

myFC = arcpy.GetParameterAsText(0) var1 = arcpy.GetParameterAsText(1) var2 = arcpy.GetParameterAsText(2) arcpy.AddField_management(myFC, "FIELD1", "TEXT", "", "", "50") arcpy.AddField_management(myFC, "FIELD2", "TEXT", "", "", "50") arcpy.CalculateField_managment(myFC, "FIELD1", "'" + var1 + "'", "PYTHON") arcpy.CalculateField_managment(myFC, "FIELD2", "'" + var2 + "'", "PYTHON")


an alternate way using an update cursor:

myFC = arcpy.GetParameterAsText(0) var1 = arcpy.GetParameterAsText(1) var2 = arcpy.GetParameterAsText(2) arcpy.AddField_management(myFC, "FIELD1", "TEXT", "", "", "50") arcpy.AddField_management(myFC, "FIELD2", "TEXT", "", "", "50") updateRows = arcpy.UpdateCursor(myFC) for updateRow in updateRows:     updateRow.FIELD1 = var1     updateRow.FIELD2 = var2     updateRows.updateRow(updateRow) del updateRow, updateRows


If this aint working.... I dunno...

Are you sure your toolbox script tool is pointing to the correct script?

View solution in original post

0 Kudos
12 Replies
ChrisSnyder
Regular Contributor III
It'd look something like this:

myFC = arcpy.GetParameterAsText(0)
var1 = arcpy.GetParameterAsText(1)
var2 = arcpy.GetParameterAsText(2)
arcpy.AddField_management(myFC, "FIELD1", "SHORT")
arcpy.AddField_management(myFC, "FIELD2", "SHORT")
arcpy.CalculateField_managment(myFC, "FIELD1", int(var1), "PYTHON")
arcpy.CalculateField_managment(myFC, "FIELD2", int(var2), "PYTHON")
0 Kudos
RobertStewart
New Contributor II
Thanks!

This is pretty much exactly what I had, except the parameters are text. int() is for integer is it not?

No matter what I seem to have used, the values stay at <Null>

The nice thing is that this confirms that I was on the right track. I'm looking through python string functions as we speak.

Cheers!
0 Kudos
ChrisSnyder
Regular Contributor III
As the name implies the arcpy.GetParameterAsText() method always brings values from the toolbox GUI into the script as string values. Three Python functions I use all the time:

convert to interger: int(myVariable)
convert to float: float(myVariable)
convert to string: str(myVariable)

Variables must be of the same type to do math operations or string concatenation. For example:

buffDist = arcpy.GetParameterAsText(0)
newBuffDist = float(buffDist) / 3.14159
arcpy.Buffer_analysis(myFC, myBuffFC, str(newBuffDist) + " METERS")
0 Kudos
RobertStewart
New Contributor II
Wow, when trying to self-teach, the simplest things escape me.
I will be keeping those notes close at hand, we do a lot of that here. Thanks for that!

However, I still seem to be getting errors. Let me write out the variables and maybe this will be more clear:

ZFile = arcpy.GetParameterAsText(0)         - Entered in the toolbox as: "Z-12-1234"
Planner = arcpy.GetParameterAsText(1)     - Entered into the toolbox as: "XX"

I want to pass these values into a featureclass that was created via the same script, into a field called: "ZFile" and "Planner" (respectively).

arcpy.CalculateField_management("MyFC","ZFile",int(ZFile),"PYTHON")

The int(Zfile) expression returns an invalid literal. I'm too new at this to understand what that means.

I 'do' appreciate the help on this. I think I'm awfully close to the end.....
0 Kudos
ChrisSnyder
Regular Contributor III
Sorry - leave off the int() part in the CalculateField tool. No sense in converting your character string to an integer! I was under the assumption your input values were integers. Not sure why I was thinking that...

This should work:

myFC = arcpy.GetParameterAsText(0)
var1 = arcpy.GetParameterAsText(1)
var2 = arcpy.GetParameterAsText(2)
arcpy.AddField_management(myFC, "FIELD1", "SHORT")
arcpy.AddField_management(myFC, "FIELD2", "SHORT")
arcpy.CalculateField_managment(myFC, "FIELD1", "'" + var1 + "'", "PYTHON")
arcpy.CalculateField_managment(myFC, "FIELD2", "'" + var2 + "'", "PYTHON")
0 Kudos
RobertStewart
New Contributor II
Hmmm. Still not working. Posted below is my code. I feel like this should have worked. I had forgotten about the quotes within quotes bit.

In modelbuilder, I was forced to use '%Variable%'. Could that be it? (yes, I'm going to have a go at it...)

I did try setting the field type alternatively to "SHORT" and "TEXT", but neither made a difference. The fields get created but not populated.

Zfile and Planner variables are set by the tool GUI.

I feel like trying to create a fully automated, fully dynamic script was maybe NOT the best idea for my first delve into python....

----



df = arcpy.mapping.ListDataFrames(mxd, "PLOCMain")[0]
addLayer = arcpy.mapping.Layer(SaveFile)
arcpy.mapping.AddLayer(df, addLayer, "TOP")

arcpy.AddField_management(addLayer,"ZFile","TEXT")
arcpy.AddField_management(addLayer, "Planner","TEXT")
 
arcpy.CalculateField_managment(addLayer, "ZFile", "'" + ZFile + "'", "PYTHON")
arcpy.CalculateField_managment(addLayer, "Planner", "'" + Planner + "'", "PYTHON")
0 Kudos
RobertStewart
New Contributor II
.....

And now I 'fully' realize why copying and pasting script is a better idea than writing my own....

arcpy.CalculateField_managEment()......

Sigh.

The last (corrected) script now produces blank fields as opposed to <Null>. I consider that a sign of improvement.
0 Kudos
ChrisSnyder
Regular Contributor III
Yes, it'd be good to change the field types to text...

How about this:

myFC = arcpy.GetParameterAsText(0) var1 = arcpy.GetParameterAsText(1) var2 = arcpy.GetParameterAsText(2) arcpy.AddField_management(myFC, "FIELD1", "TEXT", "", "", "50") arcpy.AddField_management(myFC, "FIELD2", "TEXT", "", "", "50") arcpy.CalculateField_managment(myFC, "FIELD1", "'" + var1 + "'", "PYTHON") arcpy.CalculateField_managment(myFC, "FIELD2", "'" + var2 + "'", "PYTHON")


an alternate way using an update cursor:

myFC = arcpy.GetParameterAsText(0) var1 = arcpy.GetParameterAsText(1) var2 = arcpy.GetParameterAsText(2) arcpy.AddField_management(myFC, "FIELD1", "TEXT", "", "", "50") arcpy.AddField_management(myFC, "FIELD2", "TEXT", "", "", "50") updateRows = arcpy.UpdateCursor(myFC) for updateRow in updateRows:     updateRow.FIELD1 = var1     updateRow.FIELD2 = var2     updateRows.updateRow(updateRow) del updateRow, updateRows


If this aint working.... I dunno...

Are you sure your toolbox script tool is pointing to the correct script?
0 Kudos
RobertStewart
New Contributor II
Indeed a good question, but yes, it is pointed correctly.

The script runs perfectly now, if I copy and paste it into a python window in ArcMap. This means I need to set the variables in the script instead of by the GUI.

Through the GUI/Toolbox, however, nothing seems to want to make the fields populate (they are blank instead of <Null>).

I sincerely thank you for helping me with this. For the sake of sanity, I may try simply have to manually enter those values into each map. Not the end of the world.

Cheers!
0 Kudos