Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am successfully using the follow script

import arcpy
from arcpy import env
import os
env.workspace = ("WORKSPACE")
fcList = arcpy.ListFeatureClasses()
print fcList

to export feature class names from a workspace.

I'd like to export the results to a CSV file (creating a simple one column list of Feature Class names); I've looked into the Python help guide but am struggling with the syntax and where to place the csv.writer and import csv functions.

How do I get the results to be written to a csv file?

share|improve this question
add comment

4 Answers

Why not something like this:

import arcpy
from arcpy import env
import csv
import os
env.workspace = ("WORKSPACE")
fcList = arcpy.ListFeatureClasses()
with open('codes.csv', 'wb') as f:
     writer = csv.writer(f)
     writer.writerows(fcList)
share|improve this answer
add comment

I would suggest taking a look at the scripts referenced in Working with Microsoft Excel in ArcGIS Desktop and the updated ArcGIS 10 script referenced in this ESRI forum post.

Both scripts contain "fallback" methods (python code) for exporting to CSV.

share|improve this answer
    
Thanks I'll have a look at this link! :-) –  Simon Jul 17 '12 at 14:34
add comment

It should look something like this:

import csv, arcpy, os
from arcpy import env

env.workspace = "C:/temp/mygeo.gdb"

    with open('C:\\temp\\test.csv', 'wb') as out_file:
       csv_writer = csv.writer(out_file, delimiter=',')
       csv_writer.writerow(('Name'))

       fclist = arcpy.ListFeatureClasses():
          for f in fclist:
             csv_writer.writerow(f)
share|improve this answer
add comment

you can achieve this with this way:

# Your code

import os
import arcpy
from arcpy import env 


env.workspace = ("WORKSPACE")
fcList = arcpy.ListFeatureClasses()
#print fcList

# Open a new file 
f = open("C:\Temp\newFile.txt", 'w')

# Loop feature class
for fc in fcList:
    f.write(str(row.getValue("id")) + ", " + str(row.getValue("name")) + ", " + 
           str(row.getValue("others")) + "\n")

f.close()

i hope it helps you...

share|improve this answer
    
I've had a test of the code above but come across invalid syntax in the last line - f.close() - –  Simon Jul 17 '12 at 14:34
    
there must be whitespace problem with f.close(), otherwise there should not be any trouble. open and close methods are at the same indentation level... –  Aragon Jul 17 '12 at 14:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.