I have converted a model to python in ArcGIS 10.3, and have found that the tool outputs a full and very very long field mapping. Is there a way to shorten this somehow? While I want all field names/types/lengths to stay the same, I don't necessarily want all fields to be output.

arcpy.TableToTable_conversion(SourceTable, TargetDB, "xxOutputTable", "", "RecordID \"RecordID\" true false false 8 Double 0 19 ,First,#,Database Connections\\\\GISDatabase.sde\\GISDatabase.dbo.v_InputTable,RecordID,-1,-1;Descript \"Descript\" true true false 65 Text 0 0 ,First,#,Database Connections\\\\GISDatabase.sde\\GISDatabase.dbo.v_InputTable,Descript,-1,-1;Status \"Status\" true true false 30 Text 0 0 ,First,#,Database Connections\\\\GISDatabase.sde\\GISDatabase.dbo.v_InputTable,Status,-1,-1;RecordName \"RecordName\" true true false 50 Text 0 0 ,First,#,Database Connections\\\\GISDatabase.sde\\GISDatabase.dbo.v_InputTable,RecordName,-1,-1;Owner \"Owner\" true true false 2000 Text 0 0 ,First,#,Database Connections\\\\GISDatabase.sde\\GISDatabase.dbo.v_InputTable,Owner,-1,-1;", "")

While the python works fine with it all there, I would like to be able to make it as simple as possible to modify if/when required, and I do find the long field mapping quite hard to read when scrolling through it across the screen.

Is there a way to simplify this expression?

  • 3
    Yes, you can create a FieldMappings object and add FieldMap objects to it. It's not the most intuitive process, but once you wrap your head around it it's quite useful. desktop.arcgis.com/en/desktop/latest/analyze/arcpy-classes/… – Emil Brundage Jan 7 '16 at 2:42
  • 1
    Or omit it entirely and suffer the consequences. If all you're after is a collection of geometries, with no regard to attribution (like building a mask for example), then you need not supply any field mappings. Every time I've used TableToTable or CopyFeatures without field mapping it works just fine; the Field Mappings object gives you a chance to change the field names on the way out. – Michael Stimson Jan 7 '16 at 4:09
  • @EmilBrundage - I've had a quick look at your link and think this might be on the right track if I can get my head around it. I'll have to have another in-depth read. – Midavalo Jan 7 '16 at 22:00
  • 1
    @MichaelMiles-Stimson - I've just tried this, leaving the field mapping blank and it's output all fields as-is. This would work I think as I can then use a Delete Fields to easily remove the fields I don't want, rather than trying to exclude them via the field mapping. – Midavalo Jan 7 '16 at 22:01
  • 1
    That works too. If you only have a few fields it could be possible to create an empty table, add the fields you want and use append. I have used that approach for feature classes with dozens of fields, of which I only want one or two, and a simplified field map or NO_TEST if the field names/types aren't being modified. – Michael Stimson Jan 8 '16 at 0:16

Using http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-classes/fieldmappings.htm as a guide, the following python works and makes adding/removing/modifying fields a lot easier than within with arcpy tool

import arcpy

def generateFieldMapping(fms, fieldName, fieldType, fieldLength, fieldPrecision, fieldScale, fieldAlias):
    # Create the FieldMap object
    fm = arcpy.FieldMap()
    fm.addInputField(SourceTable, fieldName)
    fm_field = fm.outputField

    # Set the different properties of the field
    fm_field.name = fieldName
    fm_field.aliasName = fieldAlias
    fm_field.type = fieldType
    if fieldType == "Text":
        fm_field.length = fieldLength
    if fieldType == "Integer" or fieldType == "Double":
        fm_field.precision = fieldPrecision
    if fieldType == "Double":
        fm_field.scale = fieldScale

    fm.outputField = fm_field

    # Add FieldMap to the FieldMappings object

    return fms

SourceDB = "Database Connections\\GISDatabase.sde"
TargetDB = "Database Connections\\GISDatabase.sde"

# Local variables:
SourceTable = SourceDB + "\\GISDatabase.dbo.v_InputTable"

fields = [
          # [Field Name, Field type, Length, Precision, Scale, Field Alias]

# Create the FieldMappings objects
fms = arcpy.FieldMappings()

for field in fields:
    generateFieldMapping(fms, field[0], field[1], field[2], field[3], field[4], field[5])

arcpy.TableToTable_conversion(SourceTable, TargetDB, "xxOutputTable", "", fms, "")
| improve this answer | |

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.