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 C000004N_37. I want to convert and return just the 37 of the string.
Asked
Active
Viewed 49 times
-3
-
Why 37? Why not 4? 4 is present in a portion of your string. – Kevin Sep 17 '15 at 19:38
-
1This is a good place to start. http://stackoverflow.com/questions/509211/explain-pythons-slice-notation – DJMcMayhem Sep 17 '15 at 19:38
-
2Will the integer you want to return always be preceded by "_"? – J. Corson Sep 17 '15 at 19:39
-
if the position of the string components is fixed, you use slicing, e.g. `n=int(s[-2:])` where s is `C000004N_37` – Pynchia Sep 17 '15 at 19:42
-
37 is the reference marker for this route. yes the int will always come after the _. The route name is always C######@_the mile post. the mile post could be 0-700 – Gus Sep 17 '15 at 19:47
-
Route could be C000125S_23 for example – Gus Sep 17 '15 at 19:50
1 Answers
1
Will this work for you:
def get_route_marker(user_string):
return int(user_string.split('_')[-1])
If all of your strings are in a list called 'list_of_strings':
route_markers = [get_route_marker(user_string) for user_string in list_of_strings]

J. Corson
- 438
- 3
- 7
-
It will work for that individual corridor's records.. But I have several hundred corridors. I was trying to figure out so it would be like int(C######N_###.split('_')[-1]) – Gus Sep 17 '15 at 20:53
-