Convert string to Int. and parse the return.

6839
6
Jump to solution
09-17-2015 12:59 PM
TerryGustafson
Frequent Contributor

I'm very new to Python.  I want to use python to convert a string to and integer and return only a portion of the string.  I have the following string type C000004N_37.  I want to convert to int and return just the 37 of the string.  C000004N_37 is a route with the following meaning. C standards for Corridor. the 6 numbers are the route number.  The N is the roadbed direction so it can be "N", "S", "E" or "W".  the underscore just creates a space and the 37 denotes the Reference Marker.  The reference marker value could be anything from 1-670.

0 Kudos
1 Solution

Accepted Solutions
EarlSarow
Deactivated User

If you need to do this just once, the simplest way is in the field calculator:

  • Open the table, right-click on the heading of the field you want to store your result in, and select "Field Calculator"
  • Select "Python" for the parser.
  • In the expression window,, enter the expression that Luke gave you above, but replace the string "C000004N_37" with your input field name, surrounded by "!" signs.  For my test, the expression looked like this:

          int(!RouteID!.split("_")[1])

If you need to do this in a Python script, on the other hand, you'll need to use an Update Cursor to step through the records, read the initial value, parse it using the "split" method, then write the results back to your output field.  There are some good examples in the ArcPy help, under "Data Access Module".

View solution in original post

6 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Terry,

Here is an example:

val = 'C000004N_37'
val = int(val.split("_")[-1])
print val
Luke_Pinner
MVP Regular Contributor

If the string is always separated by an underscore, you could use the string "split()" method:

int('C000004N_37'.split('_')[1])

This splits the string into a list with two elements at the underscore, selects the 2nd element (python lists are numbered starting from 0) and converts that to integer.

TerryGustafson
Frequent Contributor

This works for that specific Corridor but I have 12000 records I need to run this on.. can I put some wild card in for the ‘C000004N_37’ that would work with any corridor route? The C will always be constant. Followed by 6 numbers. Then a letter of either N,S,E or W. then the underscore and a Reference Marker number of 1-670

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Strings are a Sequence Type in Python, which means they support indexing and slicing.  Since you have a standard number of characters to the left and variable number to the right of the underscore, you can just slice off the right-hand side of the string.

val = 'C000004N_37'
val = int(val[9:])
EarlSarow
Deactivated User

If you need to do this just once, the simplest way is in the field calculator:

  • Open the table, right-click on the heading of the field you want to store your result in, and select "Field Calculator"
  • Select "Python" for the parser.
  • In the expression window,, enter the expression that Luke gave you above, but replace the string "C000004N_37" with your input field name, surrounded by "!" signs.  For my test, the expression looked like this:

          int(!RouteID!.split("_")[1])

If you need to do this in a Python script, on the other hand, you'll need to use an Update Cursor to step through the records, read the initial value, parse it using the "split" method, then write the results back to your output field.  There are some good examples in the ArcPy help, under "Data Access Module".

TerryGustafson
Frequent Contributor

This sounds like exactly what I want to do.. I will try it in the morning when I return to work. Thank you..

0 Kudos