Within a chosen shapefile I have the prefix, Area, that needs to be joined to the user specified attribute, chosen as a dropdown from a column within the shapefile. which will become the suffix within a new field. For example, if the user chooses 'gridcode', then the end result should be Area1, Area2, Area3, etc for each of the rows within the Zone attribute column. I have tried to use the CalculateField_management and update cursor, but both gave me errors and since I am relatively new to python, I wanted some input to help me along. Sorry for not inserting the information below in python.
Input_Shapefile = arcpy.GetParameterAsText(0)
ID = arcpy.GetParameterAsText(1)
# Process: Add Zone
arcpy.AddField_management(Input_Shapefile, "Zone", "TEXT", "20", "2", "20", "", "NULLABLE", "NON_REQUIRED", "")
# Process: Name Zones
arcpy.CalculateField_management(Input_Shapefile, "Zone", 'Area' + str(ID), "PYTHON", "")
# Tried to use update cursor, but it created the output as AreaGRIDCODE rather than the gridcode values
# with arcpy.da.UpdateCursor(Input_Shapefile, "Zone") as cursor:
# for row in cursor:
# row.setValue("Zone", 'Area' + str(ID))
# cursor.updateRow(row)
# del cursor
# I found this option below in my searches as it appeared similar to my needs, but still no luck
# ZoneRows = arcpy.UpdateCursor(Input_Shapefile)
# Zones = ZoneRows.next()
# while Zones:
# Zones.setValue('Zone', "Area" + str(ID))
# ZoneRows.updateRow(Zones)
# Zones = ZoneRows.next()
# del Zones, ZoneRows
Solved! Go to Solution.
when you want to retrieve values from other fields in a row, you need to refer to the row index based on the rows you imported. UpdateCursor—Help | ArcGIS for Desktop
So if you import a row and are using two fields in the cursor, then you need to access them as row[0] and row[1] where these represent the columns/fields that you used. The second example in that link uses
fields = ['WELL_YIELD', 'WELL_CLASS']
so, row[0] would be 'WELL_YIELD' . there are alternate naming conventions, but I will leave that out for now.
PS, the simplest way to concatenate with spaces is to ensure that the field values are string, or converted to string, then add a space
str(row[0]) + " " + row[1] ... where row[0] is a number and row[1] is already text. This can also be done with
out = "{ } { }".format(row[0], row[1]) which will handle the string thing and allow you to put in as many spaces and other stuff you want
when you want to retrieve values from other fields in a row, you need to refer to the row index based on the rows you imported. UpdateCursor—Help | ArcGIS for Desktop
So if you import a row and are using two fields in the cursor, then you need to access them as row[0] and row[1] where these represent the columns/fields that you used. The second example in that link uses
fields = ['WELL_YIELD', 'WELL_CLASS']
so, row[0] would be 'WELL_YIELD' . there are alternate naming conventions, but I will leave that out for now.
PS, the simplest way to concatenate with spaces is to ensure that the field values are string, or converted to string, then add a space
str(row[0]) + " " + row[1] ... where row[0] is a number and row[1] is already text. This can also be done with
out = "{ } { }".format(row[0], row[1]) which will handle the string thing and allow you to put in as many spaces and other stuff you want
Considering I will not know the fields of the shapefile being used, I do not think the update cursor will work. If the fields were static, it would be easier to code, however the chosen attribute field could be ID, Gridcode, GRIDCODE, Sample, Number, etc...so I am trying to come up with a better solution.
I worked through it and used your example, but it lead me to another problem. The column that was chosen for ZoneID was gridcode which gave me a result of Zone1.0 when I would rather have it as Zone1 so I had to set it up as an integer.
Input_Shapefile = arcpy.GetParameterAsText(0)
ZoneID = arcpy.GetParameterAsText(1)
fields = ['Area', ZoneID]
with arcpy.da.UpdateCursor(Input_Shapefile, fields) as cursor:
for row in cursor:
row[0] = 'Zone' + str(int(row[1]))
cursor.updateRow(row)
setting it up as an int first is fine as long as you don't have 1.2 , 1.3 etc then you may want to do
str(int(row[1]*10)) to scale all entries up by 10... you can get stupidly fancy...but always keep stuff simple, there will always be an exception, which others can deal with
If you don't know the fields or feature class ahead of time use GetParameterAsText, see below
import arcpy Input_Shapefile = arcpy.GetParameterAsText(0) field1 = arcpy.GetParameterAsText(1)#field with wanted attribute field2 = arcpy.GetParameterAsText(2)#field with wanted attribute field3 = arcpy.GetParameterAsText(3)#field to update fields = [field1,field2,field3] with arcpy.da.UpdateCursor(Input_Shapefile, fields) as rows: for row in rows: row[2] = "{ } { }".format(row[0], row[1]) rows.updateRow(row)