-2

I have the string

var = '7 msecs'
#var = int(var)
#var = float(var)

And I want to convert that to an integer (7), or float (7.0). This conversion works in javascript, as it seems to take the first value before the following, non-numerical values.

How can I do this in Python?

I want a good way to do this, without having to do

var = int(var.split(' ')[0])

It should also work for:

'10mm' or '10.55€' 

Considered duplicate (Python: Extract numbers from a string)

This question is different, as I'm trying to replicate the ability of parseInt/parseFloat in javascript which only returns the numbers at the beginning of a string. This other question is looking for all numbers in a string.

#What I want:
'123mm' -> 123
'123mm1' -> 123
Community
  • 1
  • 1
Islarf
  • 159
  • 3
  • 11
  • the measurement is always in 'msecs'? – Trimax Aug 08 '16 at 09:34
  • 1
    `int(var.split()[0])` – Chris_Rands Aug 08 '16 at 09:34
  • @Trimax well, no, its just msecs in my example as it's what I'm working with at the moment – Islarf Aug 08 '16 at 09:36
  • Using RegEx is a way but isn't more pythonic; you must import the regex library. – Trimax Aug 08 '16 at 09:36
  • @Chris_Rands Yeah, I have considered that, but I was trying to avoid it because it doesn't look very pythonic – Islarf Aug 08 '16 at 09:36
  • 1
    I think using `split` is quite pythonic, you don't need the white space, just empty parentheses splits on all white space and newlines – Chris_Rands Aug 08 '16 at 09:40
  • are the digits always going to be first in the string? – Padraic Cunningham Aug 08 '16 at 09:41
  • @PadraicCunningham yeah, in this case, because its trying to extract the number from the start of a measurement. It could also be used for currency if you keep the symbol at the end of the string – Islarf Aug 08 '16 at 09:43
  • @Chris_Rands how could you use it if there is no space, '10mm', or '10.55€' – Islarf Aug 08 '16 at 09:43
  • provide 2-3 examples – Rakesh Kumar Aug 08 '16 at 09:44
  • you can get the float by using the regex pattern `^(\d+(\.\d+)?)`, then you get int() if you need. – Trimax Aug 08 '16 at 09:44
  • If you can have or integers or floats then you might be best off using a regex – Padraic Cunningham Aug 08 '16 at 09:46
  • 1
    Possible duplicate of [Python: Extract numbers from a string](http://stackoverflow.com/questions/4289331/python-extract-numbers-from-a-string) – DocZerø Aug 08 '16 at 09:46
  • @Kristof in a way, but with this i'm trying to not use regex. – Islarf Aug 08 '16 at 09:47
  • @Islarf The answers to that question contain other techniques than just RE. – DocZerø Aug 08 '16 at 09:49
  • @Kristof It's not an exact duplicate. as in this, I'm trying to duplicate the ability of parseInt(str) in Javascript. If I had the string ('123msecs2') I don't want the 2 at the end back. the Javascript parseInt, will just return 123. This other question wants to reap all numbers in a string. I'm trying to get only the numbers at the BEGINNING of a string. parseInt will return NaN on ('hi123hi') – Islarf Aug 08 '16 at 09:55

4 Answers4

1

If you have white space, use split:

>>> s = "123 msecs2"
>>> int(s.split()[0])
123

If you want to take the numbers from the start of a string only without white space (or using regex), try this:

>>> s = "123msecs2"
>>> int("".join(list(next(iter(())) if not x.isdigit() else x for x in s)))
123

Note, it takes inspiration from the discussion here about breaking out of list comprehensions.

Community
  • 1
  • 1
Chris_Rands
  • 38,994
  • 14
  • 83
  • 119
-1

If   var = '7 msecs'

By filter:

int(filter(str.isdigit, var))

By Regex:

import re
p = re.compile(ur'\d')
re.search(p, var)

Or By join:

s = ''.join(x for x in var if x.isdigit())
print int(s)
7
bhansa
  • 7,282
  • 3
  • 30
  • 55
-1

This is already answered here: Python: Extract numbers from a string

You could use a regular expression to extract all numbers from a string:

>>> import re
>>> s = "7 msecs"
>>> re.findall(r"\d+", s)
["7"]

If you only want the first number from the string as int, you could use:

>>> import re
>>> s = "7 msecs"
>>> int(re.findall(r"\d+", s)[0])
7
Community
  • 1
  • 1
pschill
  • 5,055
  • 1
  • 21
  • 42
-1

If you want to avoid importing regex, you can try using isdigit(). But for what it's worth, I'd prefer int(var.split()[0]) to this:

var = '7 msecs'
numeric_end = 0
for i in xrange(len(var)):
    if not var[i].isdigit():
        numeric_end = i
        break

print int(var[:numeric_end])

I chose to track the index of the first non-digit character instead of building a string for slight memory/performance reasons. This also only extracts the first number in the string.

This will of course, only work for ints. For a float, you would have to also accept the . character (and probably keep track of whether or not you already have to determine if it's a valid float string).

Karin
  • 8,404
  • 25
  • 34