I am creating a line shapefile from a csv in a Python (2.7) script. I am assigning an ID field from the csv to a variable and then later using it in the Calculate Field tool. The ID field originally looks like this '011947-OCT-084' which ArcGIS(10.1) does not seem to like, so I removed the '-'s and when I use that variable in the tool I get an error:
ExecuteError: ERROR 999999: Error executing function.
Expected end of statement
Failed to execute (CalculateField).
Here is my code ("temp" is the shapefile temporarily stored in in_memory):
with open(inCSV, 'rb') as input:
with open(outCSV, 'wb') as output:
csvwriter = csv.DictWriter(output, headers, delimiter = ',')
csvwriter.writeheader()
for row in csv.DictReader(input):
uniqueID = row['Common_id']
newID = uniqueID.replace("-", "")
arcpy.AddField_management("temp", "uniqueID", "TEXT")
arcpy.CalculateField_management("temp", "uniqueID", newID)
row['Common_id'] = newID
csvwriter.writerow(row)
I'm not entirely sure what the cause behind this is, as I've used variables in the Calculate Tool field before without a problem. Thoughts?
1 Answer 1
Try:
arcpy.CalculateField_management("temp", "uniqueID", repr(newID), "PYTHON_9.3")
The default language is VBA, so need to specify that, and newID
probably isn't properly quoted as a Python expression so repr()
will take care of that.
-
1Fantastic, I'm not sure I've ever heard of the repr() function, but this will come in handy a lot!AlmaThom– AlmaThom2013年09月20日 18:15:58 +00:00Commented Sep 20, 2013 at 18:15
Explore related questions
See similar questions with these tags.