Select to view content in your preferred language

StopIteration: iteration not started (Python 2.7)

983
4
Jump to solution
01-23-2023 08:39 AM
NinaBaksh
New Contributor II

I need to add about 500 values (street names) to a domain called streetxx. The street names are in a csv file called streetname.csv. I'm using AarcMap 10.8.2. My code is very simple yet I'm getting a StopIteration error. What am I doing wrong? Thanks for the help in advance.

import arcpy
import csv

# Set the path to the CSV file and the geodatabase
csv_file = r"J:\OMS_Upgrade\Water\streetname.csv"
gdb = r"J:\OMS_Upgrade\OMSTest.mdb"

# Set the name of the feature class and field to be updated
fc = "Water_Mains"
field = "Streetxx"

# Use the arcpy.da.UpdateCursor() method to update the feature class
with arcpy.da.UpdateCursor(gdb + "\\" + fc, [field]) as cursor:
    # Open the CSV file and read the values
    with open(csv_file, "r") as f:
            reader = csv.reader(f)
            # Skip the header row
            next(reader)
            # Iterate over the rows in the CSV file
            for row in reader:
            # Update the field with the value from the CSV file
            cursor.updateRow([row[0]])

 

Here is the error message:

Traceback (most recent call last):
File "J:\OMS_Upgrade\Scripts\Update_Domain.py", line 23, in <module>
cursor.updateRow([row[0]])
StopIteration: iteration not started

 

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

Ah, I somehow missed the part where you say that you want to update the domain.

In ArcGIS, domains are handled differently from tables. Notably here, the arcpy.da.*Cursor classes only work on tables/feature classes, not on domains. To work with domains, you need the tools from the Data management/Domains toolset.

 

Something like this should work:

import arcpy
import csv

# set the variables
csv_path = r"G:\ArcGIS\data\streetname.csv"
gdb = r"G:\ArcGIS\projects\Test\Test.gdb"
domain_name = "TestDomain"

# start the csv reader and skip header
with open(csv_path, "r") as csv_file:
    reader = csv.reader(csv_file)
    next(reader)
# loop over the csv rows and write the values into the domain
    for code, description in reader:
        arcpy.management.AddCodedValueToDomain(gdb, domain_name, code, description)

Have a great day!
Johannes

View solution in original post

4 Replies
JohannesLindner
MVP Frequent Contributor

You iterate over the csv file, but arcpy expects you to iterate over the UpdateCursor.

Are you sure UpdateCursor is what you need? It seems like you could actually want InsertCursor.


Have a great day!
Johannes
0 Kudos
NinaBaksh
New Contributor II

Johannes, my day is now improving. Thank you for helping me with this.

I replaced UpdateCursor with InsertCursor and there is no longer an error.

However, the Coded Values did not get updated. I've attached an image of 3 coded values I entered manually beside my list my streetname.csv

Am I missing a step?

Thanks,

Nina

0 Kudos
JohannesLindner
MVP Frequent Contributor

Ah, I somehow missed the part where you say that you want to update the domain.

In ArcGIS, domains are handled differently from tables. Notably here, the arcpy.da.*Cursor classes only work on tables/feature classes, not on domains. To work with domains, you need the tools from the Data management/Domains toolset.

 

Something like this should work:

import arcpy
import csv

# set the variables
csv_path = r"G:\ArcGIS\data\streetname.csv"
gdb = r"G:\ArcGIS\projects\Test\Test.gdb"
domain_name = "TestDomain"

# start the csv reader and skip header
with open(csv_path, "r") as csv_file:
    reader = csv.reader(csv_file)
    next(reader)
# loop over the csv rows and write the values into the domain
    for code, description in reader:
        arcpy.management.AddCodedValueToDomain(gdb, domain_name, code, description)

Have a great day!
Johannes
NinaBaksh
New Contributor II

I can't thank you enough Johannes. The above solution works great!

0 Kudos