N
N
Nikodad2020-05-22 17:35:12
Python
Nikodad, 2020-05-22 17:35:12

How to filter real numbers from string in python?

It is necessary to make a filter for real numbers. What method can be used for this? I found that 3 methods can be suitable for integers:
isdigit()
isnumeric()
isdecimal()
But nothing for fractions. How can I do that?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kuts, 2020-05-22
@Nikodad

Regulators as an option:

import re
data = ['erwer', 'dd', '44j', '5.09', '33', 'dfgdfs', '99.100', '5645..99', '.434', '42342.']
[x for x in data if re.match('^\d+(\.\d){0,1}\d*$', x)]

['5.09', '33', '99.100']
Or the simplest procedure:
def is_float(value):
    try:
       float(value)
    except ValueError:
       return False
    return True

>>> is_float('65456')
True
>>> is_float('65456.00')
True
>>> is_float('65456.00gfghf')
False

T
Timur Pokrovsky, 2020-05-22
@Makaroshka007

https://defpython.ru/proverit_yavlyaetsya_li_strok...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question