AnsweredAssumed Answered

Export selected records to CSV using python (IDLE)

Question asked by t.faull on Mar 22, 2017
Latest reply on Jul 27, 2018 by friendde

Hello all!

I have a polygon shapefile. It contains 200 records and a (long) field with reference numbers (type codes) 0, 1, 2 or 3.

I have a point shapefile with 25,000 records. The points fall inside the various polygons throughout the map.

I want to-

Select all points that intersect the polygons that have a type code of 0, and export this selection of points to a new CSV file.


Then repeat this process for ref number = 1, 2 and 3...

4 seperate scripts (one for each ref number) or 1 script doing it all at once: either would be fine.


I want to do all this using the IDLE python GUI rather than going into ArcMap. So I want the script to point to shapefiles on my hard drive raher than layers in the Table Of Contents.


I'm still a noob at python so all I have on my mind so far is:


# Import the required python modules (I still don't quite know what this does)

??? import arcpy ???

??? from arcpy import env ???


# set the workspace environment

??? env.workspace = "C:\Data\" ???


# Select all polygons where type code = 0 and export it to a temporary feature class

arcpy.Select_analysis('C:\Data\Polygon.shp', r'\\somernt\curo\users\faullt\ArcGIS\Default.gdb\Type_code_0_polygon', '"Type_code" = 0')


# select all points that intersect the new 'type code = 0' feature class

arcpy.SelectLayerByLocation_management('C:\Data\Points.shp', 'INTERSECT', 'Scheme- unknown or no restriction', '#', 'NEW_SELECTION', 'NOT_INVERT')


# export the new selection of points to a CSV file in C:\Data

??? ???


# display a completion message

print "CSV created"



The actual lines of python I have typed above are taken from dragging the results in the geoprocessing results pane into an ArcMap python window...


Any help would be much appreciated!