To All,Often times I have a shapefile that I need to "export" using different field names and field order. To do that, I developed a python script that lets me easily change the input and output field names and specify the order. I've included a zip file with some data as an example, just unzip everything to the same folder and run the python script... Hope this is helpful... (EDIT: This should have been a discussion, not a question...sorry...)
import arcpy
def MainScript():
global arcpy
try:
# The input feature class (inputFC) can be either relative path or hard-coded path
inputFC = "CenterlinesOriginal.shp"
# The folder name specified in the outputFC_Path must exist before running the script
# If you want the new shapefile to exist in the same directory as the script, just make outputFC_Path = ""
# If you want to store it in a file geodatabase, set the outputFC_Path = "OHMarionCountyData.gdb/OHMarionCountyDataset"
outputFC_Path = "GISDataSubFolder"
# This is the name of the new shapefile or feature class
outputFC_Name = "CenterlinesFinal.shp"
# Specify any WHERE conditions as necessary
outputFC_WhereClause = ""
# Code that sets the field mappings, adjust as needed in the GetFieldMaps() function
fcFieldMaps = GetFieldMaps()
fieldmappings = GetFieldMappings(inputFC, fcFieldMaps)
# Set overwrite option
arcpy.env.overwriteOutput = True
# Output the new shapefile using the FeatureClassToFeatureClass tool
arcpy.FeatureClassToFeatureClass_conversion(inputFC, outputFC_Path, outputFC_Name, outputFC_WhereClause, fieldmappings, "")
print "DONE!!!"
except Exception, err:
print err
#------------------------------------
def GetFieldMaps():
# First column is the original field name
# Second column is the desired field name
# Third column is field type
# Fourth column is field length
# I put extra spaces in to line up all the columns and make it easier to read.
# !!! Be sure to put extra spaces between the quotes and comma's, not between field name and quotes
# See first line below for example of how NOT to do it...
fcFieldMaps = []
# fcFieldMaps.append(["ST_PREFIX ", "STR_PRE ", "String ", 2]) # !!! Extra spaces in WRONG place !!!
fcFieldMaps.append(["ST_PREFIX" , "STR_PRE" , "String" , 2]) # Correct use of extra spaces...
fcFieldMaps.append(["ST_NAME" , "STR_NAME" , "String" , 30])
fcFieldMaps.append(["ST_TYPE" , "STR_TYPE" , "String" , 4])
fcFieldMaps.append(["ST_SUFFIX" , "STR_SUFFIX" , "String" , 2])
fcFieldMaps.append(["ST_SUFFIX2" , "STR_SUFIX2" , "String" , 2])
fcFieldMaps.append(["LEFTFROM" , "L_ADD_FROM" , "Long" , 0])
fcFieldMaps.append(["LEFTTO" , "L_ADD_TO" , "Long" , 0])
fcFieldMaps.append(["RIGHTFROM" , "R_ADD_FROM" , "Long" , 0])
fcFieldMaps.append(["RIGHTTO" , "R_ADD_TO" , "Long" , 0])
fcFieldMaps.append(["LSN" , "LSN" , "String" , 64])
fcFieldMaps.append(["LCOMM" , "L_CITY" , "String" , 30])
fcFieldMaps.append(["RCOMM" , "R_CITY" , "String" , 30])
fcFieldMaps.append(["LSTATE" , "L_STATE" , "String" , 2])
fcFieldMaps.append(["RSTATE" , "R_STATE" , "String" , 2])
fcFieldMaps.append(["LEFTZIP" , "L_ZIP" , "String" , 5])
fcFieldMaps.append(["RIGHTZIP" , "R_ZIP" , "String" , 5])
fcFieldMaps.append(["LCOUNTY" , "L_COUNTY" , "String" , 3])
fcFieldMaps.append(["RCOUNTY" , "R_COUNTY" , "String" , 3])
return fcFieldMaps
#------------------------------------
def GetFieldMappings(InputFeatureClass, FieldMapsList):
'''
This function returns a FieldMappings object FOR ONLY ONE input feature class
'''
try:
fieldmappings = arcpy.FieldMappings()
for field in FieldMapsList:
fldmap = arcpy.FieldMap()
fldmap.addInputField(InputFeatureClass, 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, err:
print err
#------------------------------------
def main():
MainScript()
if __name__ == '__main__':
main()
Jason MillerNational Institute for Public Safety Technologywww.nipst.org