-1

lets say I have a a string such as :

line = "tree length for XI:      31.0215"

I would liek to only keep the 31.0215 part. I tried:

print (re.sub("^[0-9]", "",line))

but it does not work, the "." is removed, does anyone have an idea?

chippycentra
  • 879
  • 1
  • 6
  • 15

2 Answers2

1

You can try this out.

line = "tree length for XI:      31.0215"
for item in line.split():
    try:
        float(item)
        print(float(item))
    except:
        pass
Sridhar Murali
  • 380
  • 1
  • 11
1
result = ''.join([i for i in line if (i.isdigit() or (i == "."))])
SuperKogito
  • 2,998
  • 3
  • 16
  • 37