3

I have field that includes 2500 records with name RASTERVALU and type Double. I want to add another field with name Qi. My expression for Qi is equal to RASTERVALU but I don't know correct syntax.

infc = "E:/gis payannameh/Pychram_tabu/extractVP/net1.shp"
arcpy.AddField_management(infc, "Qi", "DOUBLE")
exp = "Qi" == "!RATERVALU!"
arcpy.CalculateField_management(infc,"Qi", exp)
asked Sep 18, 2021 at 9:16
1
  • 1
    You've used an equivalence operator, so exprrsion is set to False, and then use undefined variable exp, which ought to be a Python syntax error. Please remember to include the error in your question. Commented Sep 18, 2021 at 18:24

1 Answer 1

5

Try this:

import arcpy
infc = r"E:/gis payannameh/Pychram_tabu/extractVP/net1.shp"
arcpy.AddField_management(in_table=infc, field_name="Qi", field_type="DOUBLE")
arcpy.CalculateField_management(in_table=infc, field="Qi", expression='!RASTERVALUE!', expression_type="PYTHON")

I almost never use Calculate Field in Python, instead arcpy.da.UpdateCursor. I think it is more versatile and easier to get the correct syntax:

with arcpy.da.UpdateCursor(infc, ['RASTERVALUE','Qi']) as cursor:
 for row in cursor:
 row[1] = row[0]
 cursor.updateRow(row)
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Sep 18, 2021 at 9:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.