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?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Sep 20, 2013 at 17:16

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.

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.