import arcpy from os import path as p fc = "C:\\Test\\Test.gdb\\Test1" CSVFile = "C:\\Test\\Test1" def TableToCSV(fc,CSVFile): fields = [f.name for f in arcpy.ListFields(fc) if f.type <> 'Geometry'] with open(CSVFile, 'w') as f: f.write(','.join(fields)+'\n') #csv headers with arcpy.da.SearchCursor(fc, fields) as cursor: for row in cursor: f.write(','.join([str(r) for r in row])+'\n') print 'Created %s Successfully' %p.basename(CSVFile)
Solved! Go to Solution.
import arcpy from os import path as p def TableToCSV(fc,CSVFile): fields = [f.name for f in arcpy.ListFields(fc) if f.type <> 'Geometry'] with open(CSVFile, 'w') as f: f.write(','.join(fields)+'\n') #csv headers with arcpy.da.SearchCursor(fc, fields) as cursor: for row in cursor: f.write(','.join([str(r) for r in row])+'\n') print 'Created %s Successfully' %p.basename(CSVFile) if __name__ == '__main__': fc = r"C:\Test\Test.gdb\Test1" csv = r"C:\Test\Test1.csv" TableToCSV(fc, csv)
import arcpy from os import path as p def TableToCSV(fc,CSVFile): fields = [f.name for f in arcpy.ListFields(fc) if f.type <> 'Geometry'] with open(CSVFile, 'w') as f: f.write(','.join(fields)+'\n') #csv headers with arcpy.da.SearchCursor(fc, fields) as cursor: for row in cursor: f.write(','.join([str(r) for r in row])+'\n') print 'Created %s Successfully' %p.basename(CSVFile) if __name__ == '__main__': fc = r"C:\Test\Test.gdb\Test1" csv = r"C:\Test\Test1.csv" TableToCSV(fc, csv)
I think the only problem is you need to add the '.csv' extension to your test file (otherwise it does not know what file type to open). This will only work if you have ArcGIS 10.1 though since it uses the data access module.
I should also add that in the above script you created the function, but you did not run it. The line that actually will run this is highlighted in blue. The input variables do not have to be named exactly like they are when you define the function either; Python only cares about what the variables are referencing.
import arcpy from os import path as p def TableToCSV(fc,CSVFile): fields = [f.name for f in arcpy.ListFields(fc) if f.type <> 'Geometry'] with open(CSVFile, 'w') as f: f.write(','.join(fields)+'\n') #csv headers with arcpy.da.SearchCursor(fc, fields) as cursor: for row in cursor: f.write(','.join([str(r) for r in row])+'\n') print 'Created %s Successfully' %p.basename(CSVFile) if __name__ == '__main__': fc = r"C:\Test\Test.gdb\Test1" csv = r"C:\Test\Test1.csv" TableToCSV(fc, csv)