Currently I am using this code to extract the "Number of fields" and "Feature Count" for the shapefiles listed in the input csv
import csv
import arcpy
from os import path
csvpath = r"D:\QAQC\List2.csv"
output = r'D:\Test\Test_2.csv'
csvdir = path.split(csvpath)[0]
with open(csvpath, "r") as csvfile:
fcs = [r[0] for r in csv.reader(csvfile)]
with open(output, 'wb') as f:
for fc in fcs:
count = len(arcpy.ListFields(fc))
fcount = arcpy.GetCount_management(fc)
csvwriter = csv.writer(f,delimiter=',')
csvwriter.writerow([fc,count,fcount])
f.close()
Along with this I would like to extract number of attributes of each shapefile. For eg; if Africa1.shp has 4 fields and 40 features, theoretically there can be 160 attribute values to Africa1.shp (but some features might not have few attributes).
So I want the output like, Africa1.shp 4 40 160
Don't know whether such a function exists in ArcPy
I want the actual number of attribute values. To explain things, i've mentioned 40 * 4 = 160 attributes
Edit:
Based on the answers below and through this Q&A Whats a Python Script/Method to use to get the number of fields with data in an attribute table?, i've almost got what i want. Two issues. 1) I don't want FID & Geometry field to get calculated. For this, ive referred to this one - https://geonet.esri.com/thread/75885 - and it works.
Only issue is couldn't able to save it to a csv file. If i remove this one piece of code
field_list = [f.name for f in arcpy.ListFields(fc) if f.type not in types and f.name.lower() not in other_geom]
and use count = arcpy.ListFields(fc), i can able to save it as csv. Don't know how to figure this out
2) Count results for fields with no null/blanks values are like, row after row. It's getting listed for all the fields. Instead how to sum the count results ?
Here is the code
import csv
import arcpy
from os import path
csvpath = r"D:\RTXQAQC\Open.csv"
csvdir = path.split(csvpath)[0]
output = r'D:\Test\Test_323.csv'
with open(csvpath, "r") as csvfile:
fcs = [r[0] for r in csv.reader(csvfile)]
with open(output, 'wb') as f:
for fc in fcs:
types = ['OID','Geometry']
other_geom = ['shape_area','shape_length']
field_list = [f.name for f in arcpy.ListFields(fc) if f.type not in types and f.name.lower() not in other_geom]
# Get list of fields
count = len(field_list)
fcount = arcpy.GetCount_management(fc)
# Set up dictionary for the fields
countdict = {}
for field in field_list:
countdict[field] = 0
# Loop through Table and see which fields are NULL or Blank
cur = arcpy.SearchCursor(fc)
for row in cur:
for field in field_list:
if not row.getValue(field) in [None, " ", ""]:
countdict[field] = countdict[field] + 1
# Print out results - shows field name and number of non NULL/Blank fields
for f in countdict.keys():
print (fc,count,fcount,f,countdict[f])
csvwriter = csv.writer(f,delimiter=',')
csvwriter.writerow([fc,count,fcount,f,countdict[f]]
And the results are
File - No of Fields(Without FID, Geometry) - Feature count - Field name - Field vaules (excluding null/blank)
D:\\Test\\shp\\sheet41.shp, 14, 87, Y, 87
D:\\Test\\shp\\sheet41.shp, 14, 87, X, 87
D:\\Test\\shp\\sheet41.shp, 14, 87, Sample No, 87
D:\\Test\\shp\\sheet41.shp, 14, 87, Cu_Method, 72
D:\\Test\\shp\\sheet42-6.shp, 7, 333, Sample No., 333
D:\\Test\\shp\\sheet42-6.shp, 7, 333, Comments, 10
D:\\Test\\shp\\sheet42-6.shp, 7, 333, Source, 0
D:\\Test\\shp\\sheet42-6.shp, 7, 333, Shape, 333