5

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?

8

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)
| improve this answer | |
4

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)
| improve this answer | |
  • The feature class name returned in the for loop should be passed as array [f] into csv_writer.writerow – Ashok Vanam Nov 29 '19 at 1:39
3

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.

| improve this answer | |
2

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...

| 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
0
import arcpy
from arcpy import env
import csv
import os
arcpy.env.workspace = r"C:\Temp\Geodatabase.gdb"
fcList = arcpy.ListFeatureClasses()
with open('FeatureClassList.csv', 'wb') as f:
    var = csv.writer(f)
    for row in fcList:
        var.writerow([row])
print "Done"

I had some issues with the other solutions offered here and was able to address them with my above code. I wanted each feature class to be listed in a row not as a column so I had to make up loop for each row.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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