PrillLake

field mapping wih TableToTable_conversion

Discussion created by PrillLake on Feb 15, 2014
Latest reply on Feb 18, 2014 by johnmdye
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)

Outcomes