0

I have three shapefiles as points that I am trying to create new fields for and fill in the fields with a string descriptor. I am then trying to merge all three point files deleting every field but keeping the type field. The code is below:

fieldnames = ['Type','Type','Type']
shapefiles = ['Flowpp1.shp','RoadPP1.shp','InletPP.shp']
expression = ["Stream","Road",'!Element!']
for i,x,j in zip(shapefiles,fieldnames,expression):
    arcpy.AddField_management(i,x,"TEXT",25,"","","","")
    arcpy.management.CalculateField(i,x,j,'PYTHON',"TEXT")

fieldMappings = arcpy.FieldMappings()
fieldMappings.addTable(shapefiles)
for field in fieldMappings.fields:
    if field.name not in ['Type']:
        fieldMappings.removeFieldMap(fieldMappings.findFieldMapIndex(field.name))

arcpy.management.merge(shapefiles,home + '/PourPoints.shp', filedMappings)

I am running into a name error when I get to the arcpy.management.CalculateField function. What am I doing wrong?

The error is:

ExecuteError: ERROR 000539: Error running expression: Stream Traceback (most recent call last): File "", line 1, in NameError: name 'Stream' is not defined

The goal is for the type filed for Flowpp1.shp to be "Stream", Roadpp1 to be "Road", and Inlet.shp to be the row equivalent value in the !Element! field.

2 Answers 2

0

I was able to find the answer to my question from ArcPy calculating string field gives ERROR 999999

The issue was in my expression list. The original did not include '' around the field value ie '"FIELD_VALUE"'.

0

Since this post two new tools have been added to the toolbox that will simplify your code. They are Add Fields and Calculate Fields. This may be of interest to others seeking similar solutions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.