I have a script that currently writes to txt and doc files that I want to be able to write to a csv..
I need a column for "Feature Class" and a column for "Animals". Under feature class i want to list the feature class and next to it I want to capture the missing animal(s) under the Animals column. I have commented the code for your convenience. Any input would be appreciated. Thanks!
Code:
import arcpy
import os
#Initialize Parameters, Set workspace parameters, and intermediate processing files
lstfc = arcpy.GetParameterAsText(0).split(";")
output = arcpy.GetParameterAsText(1)
arcpy.env.workspace = "in_memory"
arcpy.env.overwriteOutput = True
animals=['dog','cat','turtle','giraffe']
fcjoin=path to join features
outfile = "in_memory\\sptjn"
# Open writing file, Begin iterating through feature class(es) supplied by user
with open(output,'w') as f:
f.write("Missing Animals By Zoo"+" "+ '\n'+'\n'+'\n')
for fc in lstfc:
f.write(" "+"************************************************"+'\n'+'\n')
totalcount=int(arcpy.GetCount_management(fc).getOutput(0))
# If the attribute field Zoo exists and the number of unique values in ZOO equals the total number of all animals write the information
if arcpy.ListFields(fc,"ZOO") and len(set([row.getValue("ZOO") for row in arcpy.SearchCursor(fc,"ZOO<>'' AND ZOO IS NOT NULL")]))==len(animals) :
f.write(" "+"Feature Class: "+str(os.path.basename(fc)).split('.')[2]+'\n'+'\n')
f.write(" "+"There are currently no missing animals in"+" "+str(os.path.basename(fc)).split('.')[2]+'\n'+'\n')
# If there are no features in the supplied featureclass write the following
elif totalcount==0:
f.write(" "+"Feature Class: "+str(os.path.basename(fc)).split('.')[2]+'\n'+'\n')
f.write(" "+"There are currently no features" +" "+ "in"+" "+str(os.path.basename(fc)).split('.')[2]+'\n'+'\n')
# If the attribute field ZOO exists, and if animal in the list is not in the featue class write the following
elif arcpy.ListFields(fc,"ZOO"):
f.write(" "+"Feature Class: "+str(os.path.basename(fc)).split('.')[2]+'\n'+'\n')
myList=set([row.getValue("ZOO") for row in arcpy.SearchCursor(fc,"ZOO<>'' AND ZOO IS NOT NULL")])
for animal in animals:
if animal not in myList:
f.write(" "+animal+'\n'+'\n')
# If the attribute field Zoo does not exist, perform a spatial join to the join features, create field map, then if the animal in the list is not in the feature class write the following
elif not arcpy.ListFields(fc,"ZOO"):
f.write(" "+"Feature Class: "+str(os.path.basename(fc)).split('.')[2]+'\n'+'\n')
fieldmappings = arcpy.FieldMappings()
fieldadmu = arcpy.FieldMap()
fieldadmu.addInputField(fcjoin,"ZOO")
fieldmappings.addFieldMap(fieldadmu)
arcpy.SpatialJoin_analysis(fc,fcjoin,outfile, "#", "#", fieldmappings)
intlist=set([row.getValue("ZOO") for row in arcpy.SearchCursor(outfile,"ZOO<>'' AND ZOO IS NOT NULL")])
for animal in animals:
if animal not in intlist:
f.write(" "+animal+'\n'+'\n')
arcpy.Delete_management("in_memory")
I need a column for "Feature Class" and a column for "Animals". Under feature class i want to list the feature class and next to it I want to capture the missing animal(s) under the Animals column. I have commented the code for your convenience. Any input would be appreciated. Thanks!