1

I am trying to read a CSV then iterate through an sde to find matching features, their fields, and then print them. There is a table in the list and I'm not able to skip over it and continue reading the CSV. I get the "IOError: table 1 does not exist" and I only get the features that come before the table.

import arcpy
from arcpy import env
import sys
import os
import csv
with open('C:/Users/user/Desktop/features_to_look_for.csv', 'r') as t1:
 objectsinESRI = [r[0] for r in csv.reader(t1)]
env.workspace = "//conn/[email protected]"
fcs = arcpy.ListFeatureClasses('sometext.*')
for fcs in objectsinESRI:
 fieldList = arcpy.ListFields(fcs)
 for field in fieldList:
 print fcs + " " + ("{0}".format(field.name))

Sample CSV rows (can't seem to post a screenshot of the excel file)

feature 1
feature 2
feature 3
feature 4
table 1
feature 5
feature 6
feature 7
feature 8
feature 9

Result

feature 1
feature 2
feature 3
feature 4

Desired Result

feature 1
feature 2
feature 3
feature 4
feature 5
feature 6
feature 7
feature 8
feature 9
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Mar 13, 2019 at 18:42
2
  • 1
    why do you set fcs to something: fcs = arcpy.ListFeatureClasses('sometext.*') and then stomp on that in the for loop? Which fcs is the real one in the loop? I don't think its the CSV reading at fault, all the lines are read into objectsinESRI correctly - print it and check. Commented Mar 13, 2019 at 18:46
  • Cross-posted as stackoverflow.com/q/55146907/820534 Commented Mar 14, 2019 at 13:33

2 Answers 2

2

Add a try\except to skip the feature classes/tables that don't exist.

for fcs in objectsinESRI:
 try: fieldList = arcpy.ListFields(fcs)
 except IOError: continue
 for field in fieldList:
 print fcs + " " + ("{0}".format(field.name))
answered Mar 13, 2019 at 23:03
1
  • This worked. I tried this route already and I'm not sure what I did but couldn't get it to work. Thanks, this is what I was looking for. Commented Mar 14, 2019 at 12:42
0

Try adding logic to omit any instance of "Table" from your list comprehension. Also, note that Search Cursors can read csv files directly, so no need to open the file with the csv package. For example:

import arcpy
csv = r'C:/Users/user/Desktop/features_to_look_for.csv'
rows = [row[0] for row in arcpy.da.SearchCursor(csv, "some_field") if "table" not in row[0]]

Alternatively, you can check for the existence of a file using Exists() and perform an action only of that file exists using an if statement. Then create a log of the files that do not exist. For example:

import arcpy, os
csv = r'X:\temp\my_csv.csv'
ws = r'conn\[email protected]'
does_not_exist = []
for row in [row[0] for row in arcpy.da.SearchCursor(csv, "some_field")]:
 path = os.path.join(ws, row)
 if arcpy.Exists(path):
 fields = [field.name for field in arcpy.ListFields(path)]
 print path + " " + ("{0}".format(fields))
 else:
 does_not_exist.append(path)
print "The following files do not exist: {0}".format(does_not_exist)
answered Mar 13, 2019 at 21:28

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.