Select to view content in your preferred language

Export file geodatabase table to scv file

2126
2
Jump to solution
03-18-2013 02:28 PM
ClaudineSicker
Occasional Contributor
How can I export a file geodatabase to csv table using python running a script from inside a map document.

Got the following code from Mathew Coyle off this forum and ran it but it did not work for me.

Other ideas?

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)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
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)

View solution in original post

0 Kudos
2 Replies
by Anonymous User
Not applicable
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)
0 Kudos
ClaudineSicker
Occasional Contributor
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)


Yup. Forgot to run the function. Thanks. Your my new BFF.
0 Kudos