Select to view content in your preferred language

arcpy.da.SearchCursor TypeError: 'field_names' must be string....

218
4
Jump to solution
2 weeks ago
ElisseDeleissegues1
New Contributor III

This is a portion of an inherited code, we have recently upgraded to a more current Enterprise.

The code worked on the older Enterprise version, he rest of the code has been updated and seems to be running fine when run as individual cells.

The Error message:

In  [12]:
Line 13:    with arcpy.da.SearchCursor(fc, fields, query) as row:

TypeError: 'field_names' must be string or non empty sequence of strings
---------------------------------------------------------------------------

The code:

 

#Extract Tax Parcels from Parcel Editing (Feature Service on Portal)

source = r"UPDATEDENTERPRISE/HOSTED//FEATURELAYER/FeatureServer/0"
fc = r"UPDATEDENTERPRISE/HOSTED//FEATURELAYER/FeatureServer/0"
CSVFile = r"S:\LOCALSERVERPATH\GISDATA.csv"
query = """("Name" NOT LIKE BLAHBLAHBLAH)"""

fields = [f.name for f in arcpy.ListFields(fc) if f.name in ('Name')]

with open(CSVFile, 'w') as f:
    f.write(','.join(fields)+'\n') #csv headers
    with arcpy.da.SearchCursor(fc, fields, query) as row:
        for row in rows:
            f.write(','.join([str(r) for r in row])+'\n') 
print("CSV File Created for GIS")

 

 I've been looking through StackExchange, StackOverflow, Esri Community, etc. for a couple of days now and have tried variations on suggestions but continue to get the same error.

Any suggestions will be very welcome. I am new to this.

Thank you

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DavidSolari
Occasional Contributor III

That snippet is an error prone and roundabout way of doing:

#Extract Tax Parcels from Parcel Editing (Feature Service on Portal)

field_name = "Name"

source = r"UPDATEDENTERPRISE/HOSTED//FEATURELAYER/FeatureServer/0"
fc = r"UPDATEDENTERPRISE/HOSTED//FEATURELAYER/FeatureServer/0"
CSVFile = r"S:\LOCALSERVERPATH\GISDATA.csv"
query = f"""("{field_name}" NOT LIKE BLAHBLAHBLAH)"""

with open(CSVFile, 'w') as f:
    f.write(field_name + '\n') #csv header
    with arcpy.da.SearchCursor(fc, field_name, query) as rows:
        for value, in rows:  # Deconstruct the single-item tuple into a single variable using the for loop
            f.write(value + '\n') 
print("CSV File Created for GIS")

This makes the intent clearer and you'll get a more obvious error if the dataset lacks a "Name" field.

View solution in original post

4 Replies
AlfredBaldenweck
MVP Regular Contributor

So, field_names required either one field or a list with a length greater than 0.

You appear to be feeding it a list, so the next question is: does that list have anything in it?

It’s possible that your fields don’t match the if statement on line 8. I’d check that out.

BlakeTerhune
MVP Regular Contributor

On line 8 where you're creating the fields list, the last part where it's checking if f.name in ('Name') seems weird. Are you intending to only make a list with exactly one value of 'Name'? As-is, I think like 8 is actually only getting fields that are "N", "a", "m", "e". If you are wanting to check equality, use an equals:

fields = [f.name for f in arcpy.ListFields(fc) if f.name = 'Name']

If you are wanting to check equality on a number of possible matches, then that tuple needs a comma after the first item:

fields = [f.name for f in arcpy.ListFields(fc) if f.name in ('Name',)]

 Or just use a list instead of a tuple:

fields = [f.name for f in arcpy.ListFields(fc) if f.name in ['Name']]
DavidSolari
Occasional Contributor III

That snippet is an error prone and roundabout way of doing:

#Extract Tax Parcels from Parcel Editing (Feature Service on Portal)

field_name = "Name"

source = r"UPDATEDENTERPRISE/HOSTED//FEATURELAYER/FeatureServer/0"
fc = r"UPDATEDENTERPRISE/HOSTED//FEATURELAYER/FeatureServer/0"
CSVFile = r"S:\LOCALSERVERPATH\GISDATA.csv"
query = f"""("{field_name}" NOT LIKE BLAHBLAHBLAH)"""

with open(CSVFile, 'w') as f:
    f.write(field_name + '\n') #csv header
    with arcpy.da.SearchCursor(fc, field_name, query) as rows:
        for value, in rows:  # Deconstruct the single-item tuple into a single variable using the for loop
            f.write(value + '\n') 
print("CSV File Created for GIS")

This makes the intent clearer and you'll get a more obvious error if the dataset lacks a "Name" field.

ElisseDeleissegues1
New Contributor III

@AlfredBaldenweck @BlakeTerhune @DavidSolari 

Thank you all so much for your input!

I went with David's solution as it is simplified from the original and makes more sense to my inexperienced eye.

In addition, the exported portal layer resulted in the Field Name being in all lower case so "Name" was not recognized. I received and error that asked if I meant "name" so I checked, and sure enough that was that problem.

Portal.jpg