简体   繁体   中英

How to write value in some specific column in ArcGIS table?

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))  

Updated The picture below is the table I have 在此处输入图片说明

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

To update a record you need the UpdateCursor method. Assuming your datasource is a dictioanry you could do

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

with arcpy.da.UpdateCursor(feature_class, ["COPROPCD","field2"]) 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 update the field2 column to the value of the key
            row[1] = dataset["value"]
            cursor.updateRow(row)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM