3

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

4

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.

1
  • 1
    Fantastic, I'm not sure I've ever heard of the repr() function, but this will come in handy a lot!
    – AlmaThom
    Sep 20, 2013 at 18:15

Your Answer

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

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