1

I'm trying to export a shapefile to a csv file using a cursor. I found this quesion asked and answered here: Exporting table to XYZ ASCII file via ArcPy?

However I only want to export several specific columns in a specific order. I have been able to get the .csv header to export correctly but can't get the cursor to provide the data for the rows in that same order.

import arcpy
# import csv module
import csv
# Set working file location
dataBase = r"C:\Users\vince.adams\Desktop\VLA_FinalProject\GEARY"
# Set output file location
outFile = r"C:\Users\vince.adams\Desktop\VLA_FinalProject\Tables"
tableEight = outFile + "\Table8.csv"
# S_GEN_STRUCT
genStruct = dataBase + "\S_GEN_STRUCT.shp"
fieldList = ["WTR_NM", "STRUCT_NM", "STRUCT_TYP", "LOC_DESC", "STRUC_DESC"]
#--first lets make a list of all of the fields in the table
fields = arcpy.ListFields(genStruct, fieldList)
field_names = [field.name for field in fields]
with open(tableEight,'wb') as outTable:
 writeOut = csv.writer(outTable)
 #--write all field names to the output file
 writeOut.writerow(fieldList)
 #--now we make the search cursor that will iterate through the rows of the table
 for row in arcpy.SearchCursor(genStruct, fieldList):
 field_vals = [row.getValue(field.name) for field in fields]
 writeOut.writerow(field_vals)
 del row

I'm using Arc 10.2.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Apr 24, 2015 at 1:35
2
  • If you use an arcpy.da cursor you can specify the order of the fields, even out of order for the table. As it stands though you should get the fields in the order of the table. Without knowing what fields and in what order you can't generically sort them unless you can define a rule; as the field names are in a list you can definitely sort that or compile it in a different order - so long as you have a way of deciding generically on the order. Commented Apr 24, 2015 at 2:43
  • Micahel, that is exactly what I'm getting. I'm pretty new to this so I'm not fully understanding what you're trying to get at. The fieldList that I created is the order I want the fields to come out in. What do I need to do to define a rule? Thanks for the help. Commented Apr 24, 2015 at 13:57

1 Answer 1

1

Since you're using ArcGIS 10.2, you can take advantage of the new SearchCursor in the Data Access module, which returns row values in the order of the fields specified. Try replacing your current cursor code with:

#--now we make the search cursor that will iterate through the rows of the table
for row in arcpy.da.SearchCursor(genStruct, fieldList):
 writeOut.writerow(row)
answered Apr 24, 2015 at 14:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.