Select to view content in your preferred language

Field Calc VB to Python help, ( will trade work)

3787
5
12-07-2010 08:16 AM
TurnerNowak
Deactivated User
Can anyone point me in the right direction of getting this field calculation to run in a python script ? If needed, I would be willing to help you out on any geospatial issues you may have.

The goal of this script is removing leading zeros from my address field.

To go from 0001205 Main St to 1205 Main St.

VB:
---------------------
Expression: str1
---------------------
dim str1 as string
dim str2() as string
str2 =split(  [SIT_FULL_S] ," ")

dim cnt as integer
dim lngvalue as long
lngvalue = clng( str2(0) )
str1 = str1 & lngvalue & " "
for cnt =1 to UBOUND(str2)
    str1 = str1 & str2(cnt) & " "
next
--------------------

Here is what I have converted to Python so far:

gp.CalculateField_management(fc + "\\Parcels.shp", "SIT_FULL_S", "myfunction(!SIT_FULL_S!)", "PYTHON", "def myfunction(str1):\n dim str1 = string()\n str2 = string()\n str2 =split( !SIT_FULL_S! ,\' \')\n cnt = integer()\n lngvalue = long()\n lngvalue = clng( str2(0) ) \n str1 = str1 + lngvalue + \' \'\n for cnt =1 to UBOUND(str2) \n    str1 = str1 + str2(cnt) + \' \'\\n next")

I think the problems may be my definiton of the function AND this VB line for cnt =1 to UBOUND(str2)
0 Kudos
5 Replies
ChrisSnyder
Honored Contributor
Fortunately Python is very good with text manipulation. All you need to do is the "lstrip" string method (to cut off a specified character of the left side of the text string):

address = "000718 Puget St. SW"
newAddress = address.lstrip("0")
print newAddress
'718 Puget St. SW'

In a Field calculator expression, it would look like this:

gp.CalculateField_management(myFC, "NEW_ADDRESS", "!FUBAR_ADDRESS!.lstrip('0')", "PYTHON")

or you feel invincible, you could overwrite the existing field:

gp.CalculateField_management(myFC, "ADDRESS", "!ADDRESS!.lstrip('0')", "PYTHON")
0 Kudos
by Anonymous User
Not applicable
Original User: mykonosman

Yes, that is much easier. Thanks for the help !
0 Kudos
DanielSheehan
Emerging Contributor
I have a similar question, though maybe easier, 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.

So my question is how do I say this expression in Python;

[GEOID] & "019"

is it?

!GEOID! & '019'

I know its not b/c it doesn't seem to work.

Also, if possible is there any way to say the GEOID field is text, calculate and then say everything in this text expression is now numeric so I can avoid using this temp field?

say;

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

Thanks,
Danny
0 Kudos
by Anonymous User
Not applicable
Original User: dms8md23

This looks like it works

long(str(!GEOID!) + '019')
0 Kudos
by Anonymous User
Not applicable
Original User: csny490

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...
0 Kudos