Select to view content in your preferred language

field mapping wih TableToTable_conversion

1270
2
02-15-2014 05:49 PM
AdamCrateau1
Regular Contributor
Hello,

I'm attempting to write a script that takes in a CSV file and utilizes an arcpy.TableToTable_conversion() to create a table in a geodatabase.  I would like to take advantage of Field Mapping functionality in arcpy so that I can reliably/consistently create a table with the same attributes. 

Based on some direction I've found on this forum, I've tried the following code.  It creates a table but does not honor the length values of the various string fields (i.e. all string fields are length = 255):

def GetFieldMaps():
    tblFieldMaps = []
    tblFieldMaps.append(["PropID"         ,"PropID"         ,"String",8])
    tblFieldMaps.append(["Taxlot"         ,"PropID"         ,"String",15])
    tblFieldMaps.append(["StreetNo"       ,"StreetNo"       ,"String",11])
    tblFieldMaps.append(["StreetNoSx"     ,"StreetNoSx"     ,"String",3])
    tblFieldMaps.append(["StreetName"     ,"StreetName"     ,"String",24])
    tblFieldMaps.append(["StreetType"     ,"StreetType"     ,"String",2])
    tblFieldMaps.append(["StreetDirPx"    ,"StreetDirPx"    ,"String",2])
    tblFieldMaps.append(["StreetDirSx"    ,"StreetDirSx"    ,"String",2])
    tblFieldMaps.append(["Zip3"           ,"Zip3"           ,"Short" ,3])
    tblFieldMaps.append(["SubName"        ,"SubName"        ,"String",40])
    tblFieldMaps.append(["Block"          ,"Block"          ,"String",3])
    tblFieldMaps.append(["Lot"            ,"Lot"            ,"String",3])
    tblFieldMaps.append(["Acres"          ,"Acres"          ,"Double",8,2])
    tblFieldMaps.append(["StatClass"      ,"StatClass"      ,"String",4])
    tblFieldMaps.append(["RMVLand"        ,"RMVLand"        ,"Long"  ,9])
    tblFieldMaps.append(["RMVImp"         ,"RMVImp"         ,"Long"  ,9])
    tblFieldMaps.append(["RMVTotal"       ,"RMVTotal"       ,"Long"  ,9])
    tblFieldMaps.append(["AssdValue"      ,"AssdValue"      ,"Long"  ,9])
    tblFieldMaps.append(["LastApprDate"   ,"LastApprDate"   ,"String",8])
    tblFieldMaps.append(["Zoning"         ,"Zoning"         ,"String",7])
    tblFieldMaps.append(["CodeArea"       ,"CodeArea"       ,"String",8])
    tblFieldMaps.append(["MultCodeArea"   ,"MultCodeArea"   ,"String",1])
    tblFieldMaps.append(["PropClass"      ,"PropClass"      ,"String",3])
    tblFieldMaps.append(["SalePrice"      ,"SalePrice"      ,"Long"  ,9])
    tblFieldMaps.append(["SaleDateText"   ,"SaleDateText"   ,"String",8])
    tblFieldMaps.append(["SaleCode"       ,"SaleCode"       ,"String",2])
    tblFieldMaps.append(["YearConst"      ,"YearConst"      ,"String",4])
    tblFieldMaps.append(["GrossLivingArea","GrossLivingArea","String",5])
    tblFieldMaps.append(["OwnerName"      ,"OwnerName"      ,"String",32])
    tblFieldMaps.append(["OwnerAdd"       ,"OwnerAdd"       ,"String",32])
    tblFieldMaps.append(["OwnerAdd1"      ,"OwnerAdd1"      ,"String",32])
    tblFieldMaps.append(["OwnerAdd2"      ,"OwnerAdd2"      ,"String",32])
    tblFieldMaps.append(["OwnerCity"      ,"OwnerCity"      ,"String",16])
    tblFieldMaps.append(["OwnerState"     ,"OwnerState"     ,"String",2])
    tblFieldMaps.append(["OwnerZip"       ,"OwnerZip"       ,"String",5])
    tblFieldMaps.append(["DeedRef"        ,"DeedRef"        ,"String",11])
    tblFieldMaps.append(["MaintCode"      ,"MaintCode"      ,"String",5])
    tblFieldMaps.append(["NbrhdCode"      ,"NbrhdCode"      ,"String",10])
    tblFieldMaps.append(["CurLeviedTax"   ,"CurLeviedTax"   ,"Double",11,2])
    tblFieldMaps.append(["SaleDate"       ,"SaleDate"       ,"Date"])

def GetFieldMappings(out_table,fieldmaplist):
    try:
        fieldmappings = arcpy.FieldMappings()
        for field in fieldmaplist:
            fldmap = arcpy.FieldMap()
            fldmap.addInputField(out_table,field[0])
            fld = fldmap.outputField
            fld.name = field[1]
            fld.aliasName = field[1]
            fld.type = field[2]
            fld.length = field[3]
            fldmap.outputField = fld
            fieldmappings.addFieldMap(fldmap)
            print "Added field %s" % (field[1])
        return fieldmappings
    except Exception as e:
        print e

tableFieldMaps = GetFieldMaps()
fieldmappings = GetFieldMappings(out_table,tableFieldMaps)

arcpy.TableToTable_conversion(input_csv,gdbfolder,table_name,fieldmappings)
Tags (2)
0 Kudos
2 Replies
AdamCrateau1
Regular Contributor
Basically what I would like to do is implement the following TableToTable command using Fieldmappings:

arcpy.TableToTable_conversion(input_csv,gdbfolder,table_name,"#",
    """PropID "PropID" true true false 8 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,PropID,-1,-1;
    Taxlot "Taxlot" true true false 15 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,Taxlot,-1,-1;
    StreetNo "StreetNo" true true false 11 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,StreetNo,-1,-1;
    StreetNoSx "StreetNoSx" true true false 3 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,StreetNoSx,-1,-1;
    StreetName "StreetName" true true false 24 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,StreetName,-1,-1;
    StreetType "StreetType" true true false 2 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,StreetType,-1,-1;
    StreetDirPx "StreetDirPx" true true false 2 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,StreetDirPx,-1,-1;
    StreetDirSx "StreetDirSx" true true false 2 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,StreetDirSx,-1,-1;
    Zip3 "Zip3" true true false 4 Short 0 3 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,Zip3,-1,-1;
    SubName "SubName" true true false 40 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,SubName,-1,-1;
    Block "Block" true true false 3 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,Block,-1,-1;
    Lot "Lot" true true false 3 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,Lot,-1,-1;
    Acres "Acres" true true false 8 Double 2 8 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,Acres,-1,-1;
    StatClass "StatClass" true true false 4 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,StatClass,-1,-1;
    RMVLand "RMVLand" true true false 4 Long 0 9 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,RMVLand,-1,-1;
    RMVImp "RMVImp" true true false 4 Long 0 9 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,RMVImp,-1,-1;
    RMVTotal "RMVTotal" true true false 4 Long 0 9 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,RMVTotal,-1,-1;
    AssdValue "AssdValue" true true false 4 Long 0 9 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,AssdValue,-1,-1;
    LastApprDate "LastApprDate" true true false 8 Text 0 8 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,LastApprDate,-1,-1;
    Zoning "Zoning" true true false 7 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,Zoning,-1,-1;
    CodeArea "CodeArea" true true false 8 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,CodeArea,-1,-1;
    MultCodeArea "MultCodeArea" true true false 1 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,MultCodeArea,-1,-1;
    PropClass "PropClass" true true false 3 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,PropClass,-1,-1;
    SalePrice "SalePrice" true true false 4 Long 0 9 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,SalePrice,-1,-1;
    SaleDateText "SaleDateText" true true false 8 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,SaleDateText,-1,-1;
    SaleCode "SaleCode" true true false 2 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,SaleCode,-1,-1;
    YearConst "YearConst" true true false 4 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,YearConst,-1,-1;
    GrossLivingArea "GrossLivingArea" true true false 5 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,GrossLivingArea,-1,-1;
    OwnerName "OwnerName" true true false 32 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,OwnerName,-1,-1;
    OwnerAdd "OwnerAdd" true true false 32 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,OwnerAdd,-1,-1;
    OwnerAdd1 "OwnerAdd1" true true false 32 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,OwnerAdd1,-1,-1;
    OwnerAdd2 "OwnerAdd2" true true false 32 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,OwnerAdd2,-1,-1;
    OwnerCity "OwnerCity" true true false 16 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,OwnerCity,-1,-1;
    OwnerState "OwnerState" true true false 2 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,OwnerState,-1,-1;
    OwnerZip "OwnerZip" true true false 5 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,OwnerZip,-1,-1;
    DeedRef "DeedRef" true true false 11 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,DeedRef,-1,-1;
    MaintCode "MaintCode" true true false 5 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,MaintCode,-1,-1;
    NbrhdCode "NbrhdCode" true true false 10 Text 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,NbrhdCode,-1,-1;
    CurLeviedTax "CurLeviedTax" true true false 8 Double 2 11 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,CurLeviedTax,-1,-1;
    SaleDate "SaleDate" true true false 8 Date 0 0 ,First,#,C:/Workspace/TSGfiles/TSGinfo.csv,SaleDate,-1,-1""","#")
0 Kudos
JohnDye
Deactivated User
I completely empathize with your situation here, but I've personally found working with FieldMap objects to be a nightmare unless you're doing just a handful of fields. There's just way too much code required to do it with ESRI's current arcpy implementation (maybe I'm doing it wrong, but I haven't seen any simple/elegant ways to do it)

What you have in your second posting is the python snippet of the TableToTable_conversion function, I'm assuming for that same table you want to repeatedly convert. Why don't you just establish variables for the inTable and outTable parameters, do a find and replace on the script to replace the inTable and outTable to replace the paths and roll with it?

ESRI really needs to simplify the FieldMap objects implementation. It's bananas how much code has to be written to map a single field.
0 Kudos