loop through joined tables / field mapping

876
0
05-21-2012 04:28 AM
VeraDiaz-Köhli
New Contributor
I have created a new table by joining two tables. Now I want to loop through this new table with a searchCursor and find records with a specific attribute, e.g. "number" = 10. But I get an error because no field with the name "number" is found. Since I have joined two tables, the name of the field is "tableA_tableB_number" (just the aliasName is still "number").
To be able to loop through this table I added a new field "number" and copied the values of the field "tableA_tableB_number" to this field. But this is quite tedious and sometimes I join tables that have already been joined and then it gets complicated...

So I tried it with field_mapping and wanted to change the name of the field to the aliasName, but it didn't work.

Here's my code (I took the field_mapping part from the tread http://forums.arcgis.com/threads/33060-field-mapping-in-python-for-ArcGIS-10?highlight=rename+field)

def GetFieldMappings(fc_in, dico):
    field_mappings = arcpy.FieldMappings()
    field_mappings.addTable(fc_in)

    for input, output in dico.iteritems():

        field_map = arcpy.FieldMap()
        field_map.addInputField(fc_in, input)
        field = field_map.outputField
        field.name = output
        field_map.outputField = field

        field_index=field_map.findInputFieldIndex(fc_in, input)
        field_map.removeInputField(field_index)

        field_mappings.addFieldMap(field_map)
        del field, field_map

    return field_mappings


def main():
    pass

if __name__ == '__main__':
    main()

    IN_TABLE = "xy"

    FieldDict = {}
    FieldDict[field.name]=field.aliasName

    Mapper = GetFieldMappings(IN_TABLE, FieldDict)

    arcpy.TableToTable_conversion(IN_TABLE, "ab.gdb", "test", "",Mapper)


    fieldList = arcpy.ListFields( "H:\\Moorinventar\\FM_OBJEKTBLAETTER\\Data\\FM_OBJEKTBLAETTER.gdb\\test4")
    for field in fieldList:
        print field.name
        print field.aliasName


The names of the fields in the new table are still the old names, not the old aliasNames.

At the end of the day I just want to be able to loop through the table and find certain records. How could I do this?

Thanks in advance
Vera
Tags (2)
0 Kudos
0 Replies