1

I'm working on script that takes several table results from the Zonal Statistics As Table tool and merges them into a single table. When merging I encounter the issue that the exported .dbf files have different lengths for a field I need, "UID". The merge takes the first table added and uses this length as the max for the merged "UID" field.

What would the syntax look like to achieve this?

Here is my code:

arcpy.Merge_management(listTable5M,outTable+"\\"+"5M_zonal_statistics_elevation_merged.dbf","UID \"UID\" true true false 25 Text 0 0,first,#")

Do I need to use the FieldMappings() object?


The first answer makes a lot of sense. I'm still not getting the result I want however.

The Merge still throws an error. Here is your code implemented into the script:

dbf_list_5m = arcpy.ListTables() arcpy.AddMessage(dbf_list_5m) max_UID_length = 0 field_mappings = arcpy.FieldMappings() for table in dbf_list_5m: fields = arcpy.ListFields(table) arcpy.AddMessage(table) for field in fields: if field.name == "UID": if field.length > max_UID_length: max_UID_length = field.length arcpy.AddMessage(max_UID_length) field_mappings.removeAll() field_map = arcpy.FieldMap() field_map.addInputField(table, field.name) field_mappings.addFieldMap(field_map) arcpy.Merge_management(dbf_list_5m,outTable1+"\\"+"5M_zonal_statistics_elevation_merged.dbf", field_mappings)

It is getting the max_UID_length (17 in this case) but still bombs out during the merge. I can successfully merge using the gp tool if I change the length manually (or add the table with the 'UID' field 17 characters in length first).

  • The FieldMappings() object is typically simpler than trying to manually manage the syntax of the string approach, although either is technically doable. – Erica Sep 13 '14 at 0:48
2
max_UID_length = 0
field_mappings = arcpy.FieldMappings()
for table in dbf_list:
    fields = arcpy.ListFields(table)
    for field in fields:
         if field.name == "UID":
              if field.length > max_UID_length:
                  max_UID_length = field.length 
                  field_mappings.removeAll()
                  field_map = arcpy.FieldMap()
                  field_map.addInputField(table, field.name)
                  field_mappings.addFieldMap(field_map)
arcpy.Merge_management(dbf_list, field_mappings)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy