NYGeog

Geography, GIS, Geospatial, NYC, etc.

Thursday, January 13, 2011

Field Calculator Python adding strings and integers with + instead the old &

Another Python tidbit from a post I made in someone else's thread: http://forums.arcgis.com/threads/18861-Field-Calc-VB-to-Python-help-%28-will-trade-work%29?highlight=python+string+integer

So I have a Unique ID field but its a text field with characters in the string (9X123456) and the create Lines from X,Y tool requires the ID field to be a number. I came up with 123456 & 01 (X is now a number) & 9 ie 9X123456 becomes 123456019. I can do this pretty easy in VB Script in the Field Calculator in a temp text field.

[GEOID] & "019"

I say this expression in Python;

This looks like it works

long(str(!GEOID!) + '019')

As Chris Snyder responded:

The Python concatenate operator is actually '+', so for example:

print "my" + " " + "dog"
'my dog'

So the expression would be:

long(str(!GEOID!) + '001' + '009')

Another simple way of "integerizing" your string unique id:

Run a frequency on your string field using the Frequency tool. Then join the frequency table to the original table. The OBJECTID field (a long integer BTW) of the frequency table gives you a 1-to-1 with the text version unique id.

You can also do something similar using cursors via Python...