This isnt a question its a post about a result! I spent some time trying to find a way to take the attribute table of a shapefile and make it a CSV from python. I looked at an ArcScript that was rather complicated and didnt work with the current geoprocessor anyways. The old forums had a couple topics on the matter as well and none of the replys were really what I wanted. I wanted to avoid using win32com to call to excel or anything like that. Thats just seems messy to me. The answer came in a flash before leaving work last night, combine gp.Describe with gp.SearchCursor. Using the CSV library that ships with Python, you can open a new file and pass list objects into it. I iterate though the rows with a search cursor and for each row, iterate through the fields with a for-in loop. Each pass of the for loop appends the value of the field for the row to a list. That list is written to the CSV as a full line in the table. Youll notice in the while loop that during each pass I create and delete the list object "line." This may not be nessessary but I wanted to be safe and not accidentally be adding to the same very long list instead of creating a new list for each line.import arcgisscripting, csv
gp=arcgisscripting.create(9.3)
output=open(r'C:\GIS Projects\sandbox\test.csv','w')
linewriter=csv.writer(output,delimiter=',')
fcdescribe=gp.Describe('C:\\GIS Projects\\sandbox\\New_Shapefile(3).shp')
flds=fcdescribe.Fields
header=[]
for fld in flds:
value=fld.Name
header.append(value)
linewriter.writerow(header)
cursor=gp.SearchCursor('C:\\GIS Projects\\sandbox\\New_Shapefile(3).shp')
row=cursor.Next()
while row:
line=[]
for fld in flds:
value=row.GetValue(fld.Name)
line.append(value)
linewriter.writerow(line)
del line
row=cursor.Next()
del cursor
output.close()This method has the drawback of giving you a field for the OID and geometry in the CSV. You can get around this by getting rid of the gp.Describe and just making a list object full of your field names and using that. I made this process for a larger script that deals with updates to a parcels feature class so I will always be exporting the same fields.header=['Id','C','D','AtoC','BtoD']
linewriter.writerow(header)
cursor=gp.SearchCursor('C:\\GIS Projects\\sandbox\\New_Shapefile(3).shp')
row=cursor.Next()
while row:
line=[]
for fld in header:
value=row.GetValue(fld)
line.append(value)
linewriter.writerow(line)
del line
row=cursor.Next()I hope this helps someone out there because it had me stumped for a good while. For some reason excel wants to open this with a blank line between each record. Im not sure why but I think it has to do with what CSV dialect you set it to. I dont set one here because I change the extension to txt and cram the table into a building permit tracking system. If you want to use this to make fancier CSVs the documentation for the CSV library is here.EDIT: Just had a thought. A list is a 0 based array, so if you want to keep gp.Describe, you can just pass the list using the syntax line[2:]. That way you add every list item except the first two (OID and Shape).