AnsweredAssumed Answered

Help Writing Selected Features to Individual CSV Files

Question asked by geohaskett on Feb 5, 2014
Latest reply on Feb 5, 2014 by hua17
Hello,

I am trying to write selected features to individual csv files based upon values in a list via Python.  In a nutshell, I have a feature clas that consists of policies that fall within 100 miles of the epicenter of earthquakes with a magnitude of 4.0 or greater.  I want to separate this data by epicenter (earthquake event) and write the features to unique csv files, (1) file per unique epicenter (earthquake event).  All the necessary data is stored within the feature class.

So far when I attempt to write to unique csv files I end up with the proper number of csv files, however all policies have been written into each csv.  In otherwords, the only difference between the csv files is the name of the file.  The data within them are all the same.

I am using ArcGIS 10.2 Desktop and Server software.

Any assistance would be great.  My code is as follows:

import csv,sys,arcpy,traceback  eqMapFGDB = "C:/arcgisserver/gisData/services/eqMap/data/eqMap_example.gdb" eqPoliciesFC = eqMapFGDB+"/eqPoliciesTemp" eqFC = eqMapFGDB+"/eq30Day_4mag_ID" eqReport = "C:/arcgisserver/gisData/services/eqMap/results/" policyList = [] eqList = [] epiField = ["EPICENTER"]   try:           arcpy.MakeFeatureLayer_management(eqPoliciesFC,"eqLyr")          # Read earthquake epicenters into list     print "Searching for valid earthquakes..."     with arcpy.da.SearchCursor(eqFC,epiField) as cursor:         for row in cursor:             eqList.append("{0}".format(row[0]))     del row     del cursor          for p in eqList:         print p         fields = arcpy.ListFields(eqPoliciesFC)         field_names = [field.name for field in fields]         eq = p         whereClause = "EPICENTER = '"+eq+"'"         arcpy.SelectLayerByAttribute_management("eqLyr","NEW_SELECTION",whereClause)         print "Writing affected policies..."         # Write to csv output file         with open(eqReport+p+"Temp.csv",'wb') as f:             w = csv.writer(f)             # Write field names to output csv file             w.writerow(field_names)               # Create search cursor to iterate through each row w/n the table             for row in arcpy.SearchCursor(eqPoliciesFC):                 field_vals = [row.getValue(field.name) for field in fields]                 # Write values to output csv file                 w.writerow(field_vals)             del row             del cursor         arcpy.SelectLayerByAttribute_management("eqLyr","CLEAR_SELECTION")  except:      tb = sys.exc_info()[2]     tbinfo = traceback.format_tb(tb)[0]     pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n     " +        str(sys.exc_type) + ": " + str(sys.exc_value) + "\n"     msgs = "ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n"     print msgs     print pymsg     print arcpy.GetMessages(1)

Outcomes