2

I have tables in a file geodatabase. I want to open and work with them in my script. The first step I appear to have to do is scripted underneath. My result of this script is all names of all tables in my Geodatabase. I would like to also extract all data within these tables. So that I have a matrix or a list of lists in python.

Is this possible?

import arcpy
from arcpy import env

env.workspace = r"D:\data\sre20821\Documents\Voorspelmodel\Geodatabases\2_Component.gdb"

datasetList = arcpy.ListTables("*")

for dataset in datasetList:
     print dataset

http://resources.arcgis.com/en/help/main/10.2/index.html#//002z00000011000000

migrated from stackoverflow.com Jan 14 '15 at 14:03

This question came from our site for professional and enthusiast programmers.

  • Why is it so damn hard to open a table from a Geodatabase in Arcpy. This should be a basic functionality. Its like nobody works with tables and arcpy. – Zuenie Dec 24 '14 at 9:17
  • Because arcpy is a poorly designed API. It's certainly not Pythonic. This is typical of ESRI APIs in my experience; they do not adopt the norms of the language's community or work to ensure the API is usable without a lot of hassle. I don't think they, as a company, see the need to. People are locked into ESRI because they start using it before the need for developer involvement arises (...although using from the GUI isn't that great of an experience, either). – jpmc26 Oct 3 '17 at 23:30
5

You can use a cursor to loop through each dataset and print each row.

import arcpy
from arcpy import env

env.workspace = r"D:\data\sre20821\Documents\Voorspelmodel\Geodatabases\2_Component.gdb"

datasetList = arcpy.ListTables("*")

for dataset in datasetList:
     with arcpy.da.SearchCursor(dataset, "*") as cur:
          for row in cur:
              print row

see: http://resources.arcgis.com/en/help/main/10.2/index.html#//018w00000011000000

1

If you want to read tabular data into a dictionary for further analysis, you could try something like this...

import csv

for file in datasetList:
    n=open(file,'r') # opens the file
    reader=csv.DictReader(n,delimiter = ',') #or whatever your delimiter is
    data={} #initializes the dictionary
    for row in reader:
        # reads the data from your table into the dictionary
        for col,val in row.iteritems():
            data.setdefault(col,[]).append(val)

This assumes 'file' contains the full file path to the table in your gdb. Else you'll need to 'path+file' in the n=open call. The result will give you a dictionary (data) with table headers as keys. If this works you should be able to...

print list(data.keys()) # to view a list of your headers

Then you can index the data by...

data['key1'][0] # first value in column 'key1'

Dictionaries are good for beginners. If you are more comfortable with Python I highly recommend the pandas module for working with tabular data. It's very powerful and easy to use. Hope this helps!

  • It looks awesome and I think that the functionality is exactly what I need. My table is however not a CSV. I have created it within ArcGIS by "Create table". The line: n=open(file,'r') does not work for me. – Zuenie Dec 24 '14 at 8:41
  • Yeah I've never really worked with those types of tables. Are your data associated with a shapefile? Then you should access the shp's dbf file and resave as a csv. – Laaaars Jan 3 '15 at 4:40

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.