nekomimi
nekomimi

Reputation: 1420

First float from a string full of garbage in Python?

what is the cleanest way to get the first valid float number (I consider int as a valid float) from a string of random garbage?

"jkyf- skj7eio9.00" -> 7.0
"r-9.98989 8JHGJHG" -> -9.98989
"kjg-§ejjkv" -> None (or some other indication that no float was found)

etc? Is it possible without loading re module?

Upvotes: 0

Views: 239

Answers (4)

Bobbeh
Bobbeh

Reputation: 68

Okay, so I read this post a few mins ago and thought I'd give it a shot so here's what I came up with:

values = set('1234567890.-')
a = "jkyf- skj7eio9.00"
for i in range(100):
    b = ''.join(ch for ch in a if ch in values)
print(b)

Hope this helped,

~bobbeh

Upvotes: 0

syntagma
syntagma

Reputation: 24344

EDIT: I've just noticed that you want to avoid using re.

import re

strings = ['jkyf- skj7eio9.00',
'jkyf- skj6.90.90eio9.00',
'r-9.98989 8JHGJHG',
'kjg-jejjkv']

regex = r'[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?'

for s in strings:
    found = re.findall(regex,s)
    # print found
    if len(found) > 0:
        print float(found[0][0])

Upvotes: 0

Robᵩ
Robᵩ

Reputation: 168736

You could try float(substring) for every substring, until you find one:

In [24]: def get_first_float(s):
   ....:     for start in range(len(s)):
   ....:         for end in range(len(s), start, -1):
   ....:             try:
   ....:                 return float(s[start:end])
   ....:             except ValueError:
   ....:                 pass
   ....: 

In [25]: get_first_float("jkyf- skj7eio9.00")
Out[25]: 7.0

In [26]: get_first_float("r-9.98989 8JHGJHG")
Out[26]: -9.98989

In [27]: get_first_float("kjg-§ejjkv")

In [28]: get_first_float("Even finds 1e6, One Million!")
Out[28]: 1000000.0

In [29]: 

Upvotes: 2

Jasper
Jasper

Reputation: 3947

Something like this should work (untested, because unpythonic approach in the first place if you want to avoid re). A potential - sign is not recognized, but you should be able to include that as well.

nums = [0,1,2,3,4,5,6,7,8,9,0]
firstfloat=[]
scanning = False
for letter in string:
  if letter in nums:
    firstfloat.append(letter)
    scanning = True
  if letter == '.' and scanning:
    firstfloat.append('.')
    continue

  if scanning and letter not in nums:
    break

print(''.join(firstfloat))

Upvotes: 0

Related Questions