I have created a new field called "SFX" in a shapefile called "myshape.shp":
input = r"C:\files\myshape.shp"
arcpy.AddField_management(input, "SFX", "TEXT", field_length=100)
There is now a field called "SFX" but there are no values in the cells. Now, in that same shapefile
, I have another field called "SUFFIX".
fields = [field.name for field in arcpy.ListFields(input)]
for field in fields:
if field == 'SUFFIX':
print field
>>>'SUFFIX'
Lets say 'SUFFIX' has values 'C', 'D', 'G', etc.... What I want to do, is for each row in 'SFX' I want that cell value to be equal to the corresponding cell value of 'SUFFIX'.
Without making complicated lists using a cursor, how would I do that using UpdateCursor or the Field Calculator?
3 Answers 3
Inserting the code shown below into your if statement should work.
# CalculateField_management (in_table, field, expression, {expression_type}, {code_block})
arcpy.CalculateField_management(in_table="myshape.shp",
field="SFX",
expression="!SUFFIX!",
expression_type="PYTHON_9.3",
code_block="")
If you are asking how to calculate SFX field = SUFFIX field then:
import arcpy
shapefile = r'C:\shapefile.shp'
fields = ['SFX','SUFFIX']
with arcpy.da.UpdateCursor(shapefile, fields) as cursor:
for row in cursor:
row[0]=row[1]
cursor.updateRow(row)
Examples like this can be found if you read the help section of da.UpdateCursor.
Right click your SUFFIX field, Field Calculator.
In the code block,
SFX = !SUFFIX!
Explore related questions
See similar questions with these tags.