How to extract multiple floats from a string of floats in Python

1 Answer

0 votes
import re

str_ = "2.809 -36.91 21.487 -493.808 5034.7001"
floats = []

tokens = re.findall(r"[-+]?\d*\.\d+|\d+", str_)
for token in tokens:
    f = float(token)
    print(f)
    floats.append(f)
    
    
print(floats)


    
'''
run:

2.809
-36.91
21.487
-493.808
5034.7001
[2.809, -36.91, 21.487, -493.808, 5034.7001]

'''

 



answered Jul 28, 2024 by avibootz

Related questions

1 answer 62 views
1 answer 69 views
1 answer 71 views
1 answer 66 views
1 answer 77 views
...