0

I have finished writing code and it does what I need without having any error. However, I am wondering if there is any option to get the same result in fewer lines, pecially the for loop inside the UpdateCursor.

I am using ArcMap 10.7.1

# -*- coding: utf-8 -*-

# @Author: Daniel Ibáñez Campos
# @Abstract:

# Import the arcpy and os modules
import arcpy
import os
import sys

#=============================================================================
# Functions 
#=============================================================================

# Define functions here

#def myfunction1( text ):
    #print "This is myfunction 1"
    #print text

#=============================================================================
# Main script
#=============================================================================

# It lets the user know that the script has started
print "Script starting"
# Set up the workspace environment
arcpy.env.workspace = r"D:\python_data"
# Overwrite files by default
arcpy.env.overwriteOutput = True 

#=============================================================================
# Call functions here
#=============================================================================

#myfunction1("Example text")

# Input feature class (shapefile)
fc = "10_and_more_acres.shp"
# Creates the new field name "CAUSE_TEXT"
newfield = "CAUSE_TEXT"
# Defines the new field type (This is a text field)
fieldtype = "TEXT"
# Validates that the new field created has a valid name
fieldname = arcpy.ValidateFieldName(newfield)
# This function clears up the field name 
print "cleaned up fieldname:", fieldname
# Call addField to create the new field
arcpy.AddField_management(fc, fieldname, fieldtype, "", "",50)
# Tells the user that the new field has been created
print "New field created!"
# This function generates the correct syntax for a where clause
delimfield = arcpy.AddFieldDelimiters (fc, "CAUSE_TEXT")
# Opens update cursor on column "CAUSE"
cursor = arcpy.da.UpdateCursor(fc, ["CAUSE_TEXT", "CAUSE"])
# For each row where the where clause is true 
for row in cursor:
    # Insert
    if row[1] == 0 and 14:
        row[0] = "Unknown / Unidentified"
    if row[1] == 1:
        row[0] = "Lightning"
    if row[1] == 2:
        row[0] = "Equipment Use"
    if row[1] == 3:
        row[0] = "Smoking"
    if row[1] == 4:
        row[0] = "Campfire"
    if row[1] == 5:
        row[0] = "Debris"
    if row[1] == 6:
        row[0] = "Railroad"
    if row[1] == 7:
        row[0] = "Arson"
    if row[1] == 8:
        row[0] = "Playing with fire"
    if row[1] == 9:
        row[0] = "Miscellaneous"
    if row[1] == 10:
        row[0] = "Vehicle"
    if row[1] == 11:
        row[0] = "Powerline"
    if row[1] == 12:
        row[0] = "Firefighter Training"
    if row[1] == 13:
        row[0] = "Non-Firefighter Training"
    #if row[1] == 14:
        #row[0] = "Unknown / Unidentified"
    if row[1] == 15:
        row[0] = "Structure"
    if row[1] == 16:
        row[0] = "Aircraft"
    if row[1] == 17:
        row[0] = "Volcanic"
    if row[1] == 18:
        row[0] = "Escaped Prescribed Burn"
    if row[1] == 19:
        row[0] = "Illegal Alien Campfire"
    # Update rows in the attribute table
    cursor.updateRow(row)
# Clear up the cursor
del cursor

# Tells user that the script has finished
print "End of script"

closed as off-topic by user2856, Midavalo Aug 4 at 15:40

  • This question does not appear to be about geographic information systems (GIS) within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

5

Use a dictionary:

The main operations on a dictionary are storing a value with some key and extracting the value given the key.

d = {1:"Lightning", 2:"Equipent use", ... }
with arcpy.da.UpdateCursor(fc, ["CAUSE_TEXT", "CAUSE"]) as cursor:
    for row in cursor:
        if row[1] in d: #Check if value of row[1] exists in dictionary
            row[0] = d[row[1]]
        #else:
        #   row[0] = "Missing"
        cursor.updateRow(row)

Not the answer you're looking for? Browse other questions tagged or ask your own question.