Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm trying to calculate a field (Unique_ID) in arcpy that will bring in a six digit number from another field (Point_ID) in the table. I want to add the state plane code to the end of the Point_ID and have it calculate in the Unique_ID code.

I tried copying it from a python snipped to get what I want and this is what it gave me.

 arcpy.CalculateField_management("SMON","Unique_ID","!PointID!","PYTHON","")

I also have a string of all the state plane codes. I need to get the end of the state plane code added to the end of the PointID. enter image description here

New Code just trying the "!PointID!" enter image description here Giving me this error: ERROR 000732: Input Table: Dataset SMON does not exist or is not supported Failed to execute (CalculateField).

I set the environment to the database doing the calcs and ended up working for just the !PointID! part of the script. When I try to add ,+ "PointID!" + Con , it kicks back: ExecuteError: ERROR 000539: SyntaxError: unexpected EOF while parsing (, line 1)

share|improve this question

closed as unclear what you're asking by PolyGeo, Fezter, BradHards, Paul, Mapperz Jan 24 at 5:14

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

    
What happens when you execute the code? –  detroit_hc Jan 22 at 18:01
    
PointID is not defined. –  joebob Jan 22 at 18:04
    
from your description you call the field "Point_ID" and in the code you have it as "PointID." Maybe that's the issue. –  detroit_hc Jan 22 at 18:05
    
Sorry, it's PointID in the script and the table. I messed it up writing in here. –  joebob Jan 22 at 18:08
1  
With this many Comments and Answers plus Chat it seems clear that the edit button should be used to try and revise your Question into something much clearer. If you do that then be sure to include your code (copy/paste, select and hit Code button) rather than a picture of it. –  PolyGeo Jan 23 at 23:33

4 Answers 4

An alternative solution may be to use a cursor:

arcpy.UpdateCursor - (http://resources.arcgis.com/en/help/main/10.1/index.html#//018v00000064000000)

Calculate Field is nice for in-application use (i.e. changing field values in ArcMap/Catalog or model builder, but cursors are much more powerful (and easier to use) when accessing attribute data in code. Try this...

    urows = arcpy.UpdateCursor(Point_Layer)
    for urow in urows:
        urow.StatePlane = 'Colorado North'
        urow.State = 'Colorado'
        urow.UniqueID = str(urow.PointID) + 'Con'
        urows.updateRow(urow)

    del urow, urows 
share|improve this answer
4  
+1 for using an update cursor instead. However, since the OP is using 10.1, I'd go with the arcpy.da version instead. –  nmpeterson Jan 22 at 20:48
    
Good point, @nmpeterson. While 'arcpy.UpdateCursor' will still work, 'arcpy.da.UpdateCursor' is much faster and an even better option. –  DonnRK Jan 22 at 20:54
    
what is the advantage to using arcpy.da? –  joebob Jan 22 at 22:40
    
@joebob I find arcpy.da is about 10X faster so I always use that over old style cursors if given the option. –  PolyGeo Jan 23 at 23:30
    
@joebob Yes, arcpy.da all the way. It has better support for closing properly with with statements. Not to mention the speed improvements and other various parameters that wasn't previously possible. –  Paul Jan 24 at 2:28

Try :

arcpy.CalculateField_management("SMON", "Unique_ID", "!PointID! + \" 1111\"", "PYTHON")

share|improve this answer
    
This returns PointIDCON in the Unique_ID field. –  joebob Jan 22 at 18:42
    
yep, my bad it should be !PointID! –  detroit_hc Jan 22 at 19:00
    
I get an error saying invalid syntax if it just goes in as !PointID!. –  joebob Jan 22 at 19:03
    
If I put quotes around it "!PointID!" it says ERROR 000732: Input Table: Dataset SMON does not exist or is not supported Failed to execute (CalculateField). –  joebob Jan 22 at 19:03

the names of the fields with Python need to be between "!". So if you write your code in Python, it should be :

arcpy.CalculateField_management(Point_Layer, "Unique_ID", "!PointID! + 'Con' ", "PYTHON")

or, if PointID is not a text field,

arcpy.CalculateField_management(Point_Layer, "Unique_ID", "str(!PointID!) + 'Con' ", "PYTHON")

share|improve this answer
    
arcpy.CalculateField_management("SMON", "Unique_ID", "!PointID! + '1111'", "PYTHON") –  detroit_hc Jan 22 at 19:04
    
All of the fields are strings. I still can't get it to just run the "PointID". –  joebob Jan 22 at 19:07
    
is the size of Unique_ID large enough to store the result ? Have you tried doing it from the field calculator in ArcMap ? Are your field "visible" when you make the feature layer ? –  radouxju Jan 22 at 19:41
    
Yes, here is the exact python snipped I get with adding "111" at the end: # Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script # The following inputs are layers or table views: "SMON" arcpy.CalculateField_management("SMON","Unique_ID","""!PointID!+"1111"""","PYTHO‌​N_9.3","#") –  joebob Jan 22 at 20:04
    
I can just use !PointID! too and it returns this: # Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script # The following inputs are layers or table views: "SMON" arcpy.CalculateField_management("SMON","Unique_ID","!PointID!","PYTHON_9.3","#") –  joebob Jan 22 at 20:05
urows = arcpy.UpdateCursor(Point_Layer)
for urow in urows:
    urow.StatePlane = 'Colorado North'
    urow.State = 'Colorado'
    urow.Unique_ID = str(urow.PointID) + str(Con)
    urows.updateRow(urow)

del urow, urows

Thanks!! This ended up working. UpdateCursor is much cleaner. I'm new so python new commands are fun to learn.

share|improve this answer
    
This looks identical to the code provided by @DonnRK. If I have not overlooked a key difference I think you should hit the green tick to Accept that Answer, and then delete this one. Your last sentence could be added as a Comment to that Accept-ed Answer if you wish too. –  PolyGeo Jan 24 at 0:24

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