Reputation: 23
What is the best way to split the following string into 6 float values. The number of decimal points will always be six.
x=' 2C 6s 0.043315-143.954801 17.872676 31.277358-18.149649114.553363'
The output should read:
y=[0.043315, -143.954801, 17.872676, 31.277358, 18.149649, 114.553363]
Upvotes: 2
Views: 76
Reputation: 8593
Assuming that you want to get -18.149649
instead of 18.149649
since that would be consistent I suggest using a regex in combination with the .findall()
function as follows:
import re
regex = '(-?[0-9]{1,}\.[0-9]{6})'
x = ' 2C 6s 0.043315-143.954801 17.872676 31.277358-18.149649114.553363'
out = re.findall(regex, x)
print(out)
Giving:
['0.043315', '-143.954801', '17.872676', '31.277358', '-18.149649', '114.553363']
Update due to comment:
You could replace [0-9]
with \d
which is equivalent since \d
matches a digit (number) as shown here.
Upvotes: 4
Reputation: 101
This should do the trick.
re.findall(r'\-?[0-9]+\.[0-9]{6}', string)
Upvotes: 2