1

I have a collection of about 200 tables, and I would like to make sure that all the fields are text before merging them into a single table using python. There is at least one field ("Number_of") which I know for sure is integer. All the tables have the exact same schema.

This Geonet thread has been really helpful, but I still can't get it.

Here is my code:

fieldList = ['Feature','Number_of','Water_Type','GlobalID']

fieldMappings = arcpy.FieldMappings()

for field in fieldList:
    vars()[field] = arcpy.FieldMap()

    outField = vars()[field].outputField
    vars()[field].type = "Text"
    vars()[field].outputField = outField

    fieldMappings.addFieldMap([field])

This current code returns an error saying, "NameError: The attribute 'outputField' is not supported on this instance of FieldMap."

Essentially, all I need to do is create a Field Map that I can use in the python Merge tool which will convert the "Number_of" field to text.

4

Well this script might be a bit convoluted but it worked for me. You may need to tweak with the fldConvTypes variable - I'm not sure if it works for Date and/or Guid field types.

Essentially it figures out all the fields between all the feature classes, and throws them in a list. It then iterates through all the fields in the feature classes once again. If the field map hasn't been added to the field mappings yet, it will add it. It then checks if the field type is a type in the fldConvTypes, and changes it to text if it is.

You can use this script to merge as many feature classes as you like. Just update your fcs variable with each feature class.

#In feature class 1
inFc1 = r"C:\Users\e1b8\Desktop\E1B8\Workspace\Workspace.gdb\test"
#in feature class2
inFc2 = r"C:\Users\e1b8\Desktop\E1B8\Workspace\Workspace.gdb\tst2"
#out feature class
outFc = r"C:\Users\e1b8\Desktop\E1B8\Workspace\Workspace.gdb\test_merge20"

#list feature classes
fcs = [inFc1, inFc2]

import arcpy

#field types to convert
fldConvTypes = ["Date",
        "Double",
        "Guid",
        "Integer",
        "Single",
        "SmallInteger"]


#field map dict
fldMapDi = {}

#add all fields to list
for fc in fcs:

    #get shape field name
    shpFld = arcpy.Describe (fc).shapeFieldName

    #iterate field objects in feature class
    for fldOb in arcpy.ListFields (fc):

        #get field name
        fld = fldOb.name

        #skip shape field
        if fld == shpFld:
            continue

        #check if field map has been created for field
        if not fld in fldMapDi:

            #create new field map
            fm = arcpy.FieldMap ()

            #add input field
            fm.addInputField(fc, fld)

            #check if field should be switched to text
            if fldOb.editable and fldOb.type in fldConvTypes:

                print "changing field", fld, "type to text"
                #change field type to text
                fldOb.type = "Text"

                #update field map output field 
                fm.outputField = fldOb

            #and field map to dict
            fldMapDi [fld] = fm

        else:
            #get field map object from di
            fm = fldMapDi [fld]

            #add input field
            fm.addInputField(fc, fld)

            #change field map object in dict
            fldMapDi [fld] = fm


##done creating field maps
#create empty field mappings
fms = arcpy.FieldMappings()

#iterate fields in dict
for fld in fldMapDi:

    #get field map from dict
    fm = fldMapDi [fld]

    #add field map to field mappings
    fms.addFieldMap (fm)

#merge
print "merging"
arcpy.Merge_management (fcs, outFc, fms)
print "created:", outFc
  • Thank you, this seems to do the trick as far as field mapping. I am running into an issue, however, when I perform the merge. It is almost like the field mapping variable isn't registering. Do have any idea why that might be? My field map seems right. I know that each desired field is added to the field map, and type is changed accordingly. But then when I run the merge tool, it fails because the one field is registering as integer when it should have been changed to text. – Olivia Aug 21 '15 at 12:51
  • It worked for my feature classes, but there may be more of a trick to it depending on field conditions somehow. Altering the code to add all similar fields into the same field map may be needed. See gis.stackexchange.com/questions/151353/… – Emil Brundage Aug 22 '15 at 4:22
  • I've posted a different version that may solve your issues. – Emil Brundage Aug 23 '15 at 18:54
  • Your second version worked perfectly. My merge tool is now working. Thank you! – Olivia Aug 28 '15 at 17:32

Your Answer

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

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