Select to view content in your preferred language

Get attributes from many shapefiles?

3832
24
Jump to solution
03-02-2017 08:40 AM
MichaelLenore1
Deactivated User

I have a very large .gdb (15000+ shapefiles). I know that's unusually and impractially large, but it's the result of an iterative model. I need an Excel sheet with all of those shapefiles' attributes. I tried merging them but it's too many. Is there some other way? 

Tags (2)
24 Replies
Lake_Worth_BeachAdmin
Frequent Contributor

seems hes just executing each statement separately in the arcmap Py windows rather than executing the entire script. 

0 Kudos
MichaelLenore1
Deactivated User

It was indented properly, and I ran it all at once - just copied and pasted above and lost formatting. 

0 Kudos
FilipKrál
Frequent Contributor

Hi Michael, 

Jonathan Quinn has a very good point about formatting code on GeoNet but he's not quite right about why you are seeing the error. In fact, the variable that refers to the list of fields is called fieldnames consistently throughout the script. I suspect the error is raised by the call to arcpy.da.SearchCursor. It should not be, but anyway, try to replace

with arcpy.da.SearchCursor(fc, fieldnames) as sc:

with this:

with arcpy.da.SearchCursor(fc, field_names=fieldnames) as sc:

Filip.

0 Kudos
JonathanQuinn
Esri Notable Contributor

Well, I'll have to object.  The error is on line 11, print(field_names), and field_names isn't defined prior to attempting to print it.  If line 11 was removed, then yes, fieldnames is used consistently and there won't be a problem.  Secondly, you don't need to use named parameters when the second parameter is the field names anyway.  If the field names parameter was not second and there were other optional parameters between the feature class and the field names parameter, then you'd do field_names=fieldnames.

FilipKrál
Frequent Contributor

Hi Jonathan,

You are quite right and I owe you an apology. I am glad you pointed this out because it indeed did not make sense to me why one would have to specify the parameter name of field_names when it can be inferred from the position.

Anyway, I see the discussion has moved on quite a bit.

Filip.

0 Kudos
MichaelLenore1
Deactivated User

OK, I ran this again, as follows: 

import arcpy
output_file = r'C:\Users\mlenore\Desktop\GIS_Working_Folder\Residential_model\bgs.csv'
arcpy.env.workspace = r'C:\Users\mlenore\Desktop\GIS_Working_Folder\Residential_model\bgs_finished_2.gdb'
print("Listing feature classes...")
feature_classes = arcpy.ListFeatureClasses()
print("Found {0} feature classes".format(len(feature_classes)))
first_fc = feature_classes[0]
fieldnames = [f.name for f in arcpy.ListFields(first_fc) if f.type.lower() != 'geometry']
delimiter = ','
print(fieldnames) #line 10
with open(output_file, 'w') as fl:
     print('Writing to {0}'.format(output_file))
 
     # write header
     fl.write(delimiter.join(fieldnames) + "\n")
     
     # append attributes from each feature class
     for fc in feature_classes:
         print(fc)
         with arcpy.da.SearchCursor(fc, field_names=fieldnames) as sc:
             for row in sc:
                 fl.write(delimiter.join(map(str, row)) + '\n')
         del row
         del sc‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

but it stopped before finishing all the feature classes and I got the following: 

Runtime error
Traceback (most recent call last):
File "<string>", line 23, in <module>
NameError: name 'row' is not defined

FYI it processed 1700 or so out of the approximately 15,000 FCs before stopping. The csv file has those correctly. 

Please let me know if you have any ideas. Thanks for this excellent script. 

0 Kudos
Lake_Worth_BeachAdmin
Frequent Contributor

Which fc did it stop at? Is there any data there or is it all NULL/ blank? 

0 Kudos
MichaelLenore1
Deactivated User

Stopped at 1783. I'm not sure what you mean about data - there is data for all of the feature classes in the source geodatabase, and there is also data in the generated csv, which is correct, as noted above. The csv has a line for each of the 1783 fcs and nothing more. 

0 Kudos
Lake_Worth_BeachAdmin
Frequent Contributor

 FC 1783 has attributes to list? It saying there is no row it can find. Anything special about this fc? 

0 Kudos
MichaelLenore1
Deactivated User

Oh, I see, sorry. Sure enough, there's no data in that FC. I guess that's why it stopped?

0 Kudos