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).

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Sep 12, 2014 at 21:44
1
  • The FieldMappings() object is typically simpler than trying to manually manage the syntax of the string approach, although either is technically doable. Commented Sep 13, 2014 at 0:48

1 Answer 1

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)
answered Sep 12, 2014 at 22:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.