Parse a string into int when the string contains a number + extra characters

In Python, how can I / should parse a string that has a number, and then other characters, into an int?

The specific problem I'm trying to solve is to parse the first number from a string containing a number, followed by an arbitrary number of other characters, including possibly other numbers that don't interest me. For example, if string "12//" I need to get only 12 .

+6
source share
5 answers

I would use this regex:

 import re try: print int(re.compile("(\d+)").match('12//').group(1)) except: print "there was no number" 

It will retrieve all digits and slings with the first non-digital character.

\d means one digit, \d+ means at least one digit matches and (\d+) together means that you found in group 1.

+10
source

If you want to extract the numbers in a string:

 int(''.join(c for c in s if c.isdigit())) 
+12
source

Looks like itertools.takewhile :

 >>> from itertools import takewhile >>> s = '12//' >>> int(''.join(takewhile(str.isdigit, s))) 12 
+9
source

This is a kind of cool technique, but it may be redundant if this is only the format you are describing:

 import string potential_bad_characters = string.ascii_puctuation + string.ascii_letters int(my_string.translate(None,potential_bad_characters )) #Or int(mystring.rstrip(potential_bad_characters)) #Or int(filter(str.isdigit,my_string)) #Or (kudos @JonClements) potential_bad_characters = ''.join(map(chr, range(256))).replace('0123456789', '') ... 
+3
source

Similar to jh314s solution, but only accepts digits before any asymmetric character (therefore 34 // 98 will become 34, not 3498). Suppose the string has a value of "s":

 nonDigitIdx = [idx for idx,ch in enumerate(s) if not ch.isdigit()][0] i = int(s[:nonDigitIdx-1]) 
+1
source

Source: https://habr.com/ru/post/950383/


All Articles