3

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

0

3 Answers 3

6

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

2

To read a table in a geodatabase to a NumPy Array you can use arcpy.da.TableToNumPyArray(in_table, field_names). Specifying a list of field names is recommended for performance reasons, but you can use '*' for all fields. The resulting NumPy Array can be converted to a Pandas DataFrame.

import arcpy
import pandas as pd

input = r'my_project.gdb\my_table'
arr = arcpy.da.TableToNumPyArray(input, '*')

# convert to a Pandas DataFrame
df = pd.DataFrame(arr)

For details, refer to Esri's arcpy.da.TableToNumPyArray documentation.

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.

0

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