Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

EDIT: Soreted, thanks :-)

I'm trying to combine a (large) number of tables into a merged table, keeping only certain fields. I've tried the below, but it still gives all the fields. Any ideas?

listEventTables = arcpy.ListTables("RS_EVENT_ALL_*")

fieldMappings = arcpy.FieldMappings()

for table in listEventTables:
    fieldMappings.addTable(table)

for field in fieldMappings.fields:
    if field.name not in ["ROUTE_ID","TRIP_NUM","OFFICE_ID","VOLUME","ACTIVITY","ARRIV_TIME"]:
    fieldMappings.removeFieldMap(fieldMappings.findFieldMapIndex(field.name))

eventTable = outGDB + "Merge" # Merge of event tables (has to be in a GDB)

arcpy.Merge_management(listEventTables, eventTable, fieldMappings)
share|improve this question

1 Answer 1

up vote 2 down vote accepted

I'm not sure why that isn't working for you, but why not try an additive approach instead of a removal approach (see code below). I confess I haven't tested this code to make sure it will work, but I'm pretty sure it will.

EDIT: I've tested this script out on a test dataset and it works fine. I'll continue investigating what the problem is with your original script.

EDIT 2: I've tested out a simplified variant of your original script step-by-step to see where it was failing, and I couldn't find anything wrong with the procedure. My one thought is that it appears that your fieldmappings.removeFieldMap() statement isn't properly indented. However, if that's the case in the script you are running, it should simply throw an error.

fieldList = ["ROUTE_ID","TRIP_NUM","OFFICE_ID","VOLUME","ACTIVITY","ARRIV_TIME"]

listEventTables = arcpy.ListTables("RS_EVENT_ALL_*")

fieldMappings = arcpy.FieldMappings()

#iterate through fields and create unique fieldmap for each
for field in fieldList:

    #creates a fieldmap with name of input field
    vars()[field] = arcpy.FieldMap()

    for table in listEventTables:
        vars()[field].addInputField(table,field)

    outField = vars()[field].outputField
    outField.name = field
    vars()[field].outputField = outField

    fieldMappings.addFieldMap(vars()[field])


eventTable = outGDB + "Merge" # Merge of event tables (has to be in a GDB)

arcpy.Merge_management(listEventTables, eventTable, fieldMappings)
share|improve this answer
    
Thanks a bunch Jay, I'll try it out tomorrow morning –  Mike Mar 6 '13 at 16:57
    
Did this work out for you? –  Jay Guarneri Mar 8 '13 at 20:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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