10 examples of 'extract number from string python' in Python

Every line of 'extract number from string python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
62def _extract_number_from(t_string):
63 return int(t_string.split()[0])
12def extractnumbers(s):
13 return tuple(map(int,re.findall("(\d+)\.(\d+)\.(\d+)",str(s))[0]))
1384def extract_number_str(str_in):
1385 """ Extract first number from a given string and convert to a float number.
1386
1387 The function works with the following formats (,25), (.25), (2), (2,5), (2.5),
1388 (1,000.25), (1.000,25) and doesn't with (1e3), (1.5-0.4j) and negative numbers.
1389
1390 Args:
1391 str_in: a string ('1,20BUY')
1392
1393 Returns:
1394 A float number (1.20)
1395 """
1396 import re
1397 # deletes all char starting after the number
1398 start_char = re.search('[*:!%$&?a-zA-Z]',str_in).start()
1399 str_num = str_in[:start_char]
1400
1401 if re.search('\d+',str_num) is None:
1402 # no number in str_num
1403 return 1.0
1404 elif re.search('^(\d+|\d{1,3}(,\d{3})*)(\.\d+)?$',str_num) is not None:
1405 #Commas required between powers of 1,000
1406 #Can't start with "."
1407 #Pass: (1,000,000), (0.001)
1408 #Fail: (1000000), (1,00,00,00), (.001)
1409 str_num = str_num.replace(',','')
1410 return float(str_num)
1411 elif re.search('^(\d+|\d{1,3}(.\d{3})*)(\,\d+)?$',str_num) is not None:
1412 #Dots required between powers of 1.000
1413 #Can't start with ","
1414 #Pass: (1.000.000), (0,001)
1415 #Fail: (1000000), (1.00.00,00), (,001)
1416 str_num = str_num.replace('.','')
1417 return float(str_num.replace(',','.'))
1418 elif re.search('^\d*\.?\d+$',str_num) is not None:
1419 #No commas allowed
1420 #Pass: (1000.0), (001), (.001)
1421 #Fail: (1,000.0)
1422 return float(str_num)
1423 elif re.search('^\d*\,?\d+$',str_num) is not None:
1424 #No dots allowed
1425 #Pass: (1000,0), (001), (,001)
1426 #Fail: (1.000,0)
1427 str_num = str_num.replace(',','.')
1428 return float(str_num)
85def get_number(s):
86 try: return int(s)
87 except: return None
29def parse_number(string):
30 """return the parsed number if any, otherwise, the string as it"""
31 try:
32 return int(string)
33 except ValueError:
34 pass
35 try:
36 return float(string)
37 except ValueError:
38 pass
39 return string
20def ParseNumber(number):
21 if number.startswith('0x'):
22 return int(number[2:], 16)
23 else:
24 return int(number)
156def get_numbers(num):
157 card_and_port = str(num)
158 card_and_port = re.split('/|-',card_and_port)
159 card = card_and_port[0]
160 fromPort = card_and_port[1]
161 toPort = fromPort if len(card_and_port) <= 2 else card_and_port[2]
162 return card, fromPort, toPort
371def strip_number(s: str) -> Tuple[int, str]:
372 m = re.match('[0-9]*', s)
373 assert m is not None
374 return int(m.group() or 1), s[m.end():]
7def parse_number(text):
8 """
9 Convert parsed text into a number.
10 :param text: Parsed text, called by :py:meth:`parse.Parser.parse()`.
11 :return: Number instance (integer), created from parsed text.
12 """
13 return int(text)
236def ExtractInt(data_string):
237 """
238 Extract int values from an ASCII string
239 """
240 values = data_string.split()
241 values = [int(v) for v in values]
242 return values

Related snippets