1

I am writing a Python script to add fields and write data into the ArcGIS table. I can successfully edit and delete columns or fields in the table. But I am stuck at writing data into the fields. I got a code by which we can get the type and others info about the fields. But I was unable to write something on that particular field.

Here is the script I got from their forum -

import arcpy

feature_class = "c:/data/counties.shp"

# Create a list of fields using the ListFields function
fields = arcpy.ListFields(feature_class)

# Iterate through the list of fields
for field in fields:
    # Print field properties
    print("Field:       {0}".format(field.name))
    print("Alias:       {0}".format(field.aliasName))
    print("Type:        {0}".format(field.type))
    print("Is Editable: {0}".format(field.editable))
    print("Required:    {0}".format(field.required))
    print("Scale:       {0}".format(field.scale))
    print("Precision:   {0}".format(field.precision))  

The picture below is the table I have

enter image description here

My Primary Key is COPROPCD and I want to change the other fields based on my other dataset which has the common primary key.

Update 2 I tried the following code but it still doesn't work and get errors.

import arcpy
 feature_class = "c:/BaseMapsLocalCopy/Export_Output.shp"

 with arcpy.da.UpdateCursor(feature_class, ['COPROPCD','LEGAL']) as cursor:
     for row in cursor:
         #Check if "COPROPCD" exist as a key in the dict     
         #if row[0] in dataset.keys():
         #logic for updating the dable
         #This will upload the field2 column to the value of the key
         if row[0] == 'R040222':
             row[1] = 'GORU'
             cursor.updateRow(row)

And the error it is giving is that there no column that was specified. The exact error is as below -

enter image description here

Final Update It works now. My file was wrong. Correcting the feature_class solved my issue.

closed as off-topic by Hornbydd, Vince, LaughU, Fran Raga, BERA May 23 at 9:25

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This problem cannot or can no longer be reproduced. Changes to the system or to the asker's circumstances may have rendered the question obsolete, or the question does not include a procedure to enable potential answerers to reproduce the same symptoms. Such questions are off-topic as they are unlikely to help future readers, but editing them to include more details can lead to re-opening." – Hornbydd, Vince, LaughU, Fran Raga, BERA
If this question can be reworded to fit the rules in the help center, please edit the question.

  • You could do this using the Calculate Field tool but depending on your precise requirements writing an UpdateCursor may be a better approach. – PolyGeo May 22 at 20:31
  • Can you show me some kind of script example? I am completely new with python and arcgis. – Naseef Ur Rahman May 22 at 20:32
  • There are many existing Q&As on this site that include such code. To find them you could review the field-calculator and/or cursor tags or simply Google because they should show up amongst the early matches. – PolyGeo May 22 at 20:36
  • Please see my updated post. I mentioned what I did so far, And I got the code from the QnA site. – Naseef Ur Rahman May 22 at 20:37
  • 3
    COPROPCD and/or LEGAL isn't a field in your table. Make sure you're referencing the right table and that you're not referencing the fields' aliases. – Emil Brundage May 22 at 20:47

Browse other questions tagged or ask your own question.