My code currently follows this format:

Create FGDB
From local folder, get list of all tables
Do TableToGeodatabase_conversion on all tables

What I want to do now is exclude certain tables from being in the TableToGeoDatabase_conversion, as well as exclude certain rows for certain tables.

Can anybody point me into the right direction in doing so?

  • 2
    Do you have any rules to exclude the tables and the rows? – Marcelo Villa Jul 9 '19 at 15:26
  • @MarceloVila - What do you mean by rules? I kind of just want to pin-point certain tables, say table1 and table2 that i want to exclude, and maybe from table3 i dont want col1 and col2? – GISProdigy Jul 9 '19 at 17:26
  • 1
    I mean how do you decide what tables and what rows to keep. Reading your comment it seems like it's an arbitrary process. – Marcelo Villa Jul 9 '19 at 18:11
  • 1
    Ok. And do you want to exclude rows or columns? You state rows in your question but your comment mentions col1 and col2. Furthermore, do these table share those columns as well? – Marcelo Villa Jul 9 '19 at 18:47
  • 1
    Alright. Please update your question to include that information along with the code you have written. – Marcelo Villa Jul 9 '19 at 19:27

You have an odd situation here. First you have to specify a list of the tables you want to exclude. Second, you have to specify the fields you want to exclude from each table. Having specified these two things, you can move forward and call arcpy.TableToGeodatabase_conversion() on all the tables but the ones you want to exclude. However, this tool does not let you specify which fields to convert and which fields to exclude. You have two options here:

  • Delete the fields from the original table before calling arcpy.TableToGeodatabase_conversion(), or
  • Delete the fields from the table stored in the GDB.

I believe the second choice is a more appropiate one. Here is a snippet that should accomplish what you are looking for:

import arcpy

arcpy.env.workspace = r'C:\folder\with\tables'
gdb = r'C:\path\to\geodb.gdb'

tables = arcpy.ListTables()

# specify tables and fields to exclude
tables_to_exclude = ['table5', 'table8', 'table9']
fields_to_exclude = {'table1': ['col1', 'col3'], 'table2': ['col2', 'col4'],
                     'table3': ['col4']}

# remove tables to exclude from table's list
tables = reduce(lambda x, y: filter(lambda z: z!=y, x), tables_to_exclude, tables)

# iterate through each table and store it in the gdb
for tab in tables:
    arcpy.TableToGeodatabase_conversion(tab, gdb)

# change workspace to gdb
arcpy.env.workspace = gdb

# iterate through each table that has fields you want to exclude and delete them
for tab in fields_to_exclude.keys():
    arcpy.DeleteField_management(tab, fields_to_exclude[tab])

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