# For each field in the object list, add the field name to the # name list. If the field is required, exclude it, to prevent errors exclude = ["UGISID", "LBL_X", "LBL_Y"] for field in fieldObjList: if not field.required: if not field.name in exclude: fieldNameList.append(field.name)
inputShapeFile = "C:/Temp/yourInput.shp" outputLocation = "C:/Temp" outputShapeFileName = "yourExport.shp" # List of the fields to keep. outputFields = ["UGISID", "LBL_X", "LBL_Y"] fieldMappings = arcpy.FieldMappings() # create the field mappings from the outputFields list. # only fields in the list will be included in the exported shape file. for field in outputFields: fieldMap = arcpy.FieldMap() fieldMap.addInputField(inputShapeFile, field) fieldMappings.addFieldMap(fieldMap) # Use FeatureClassToFeatureClass and apply the fieldMappings arcpy.FeatureClassToFeatureClass_conversion(inputShapeFile, outputLocation, outputShapeFileName, "", fieldMappings, "")
Hi Mark,
Thanks for the above.
Couple of questions (assume I know only a little Python, thanks)
Thanks,
Doug
There are so many different ways of tackling this issue, i.e., the "SQL LIKE with Python strings," that I encourage you to hit up Google or some other search engine. The question you have isn't really an ArcPy question, it is a general Python question, that has lots of answers floating around the web.
Thanks Josh. And thanks for keeping an eye on old threads.
I agree on all points.
I'm in the "gathering information" step before writing any code. I will take a look at other sources.
I need to remove unneeded data from multiple shapefiles saved in a .gdb.
Since the data originates as .shp files, I thought it would be best to work with arcpy and tap the GIS community.
Cheers,
Doug
Josh,
Is there a way to keep the file name but add a suffix. For example, if my file is named XYZ.shp and I want to remove some fields and name the new file, XYZsubset.shp? I want to use the inputShapeFile name as part of the outputShapefile name. Thanks
The following is an example of how you can append a suffix to a file name:
>>> orig_name = "XYZ.shp"
>>> suffix = "subset"
>>> p, x = orig_name.rsplit(".", 1)
>>> new_name = "{}{}.{}".format(f, suffix, x)
>>> new_name
'XYZsubset.shp'
>>>
Thanks Josh!