Reputation: 23
I have a string with specific format and I'd like to extract its data into python array. What is the regEX string format for this?
The string
417,364.4265,2535.2258,16.7616,143.5451,0,0 ; Leviathan
Array
arr = ['417', '364.4265', '2535.2258', '16.7616', '143.5451', '0', '0', 'Leviathan']
Upvotes: 1
Views: 171
Reputation: 1
if U can add space between the numbers U can use the split method
s=417 364.4265 2535.2258 16.7616 143.5451 0 0 Leviathan
s=s.split()
output:
['417','364.4265','2535.2258','16.7616','143.5451','0','0','Leviathan']
it's easier and U don' need to put extra libraries
Upvotes: 0
Reputation: 43169
You could use
import re
string = "417,364.4265,2535.2258,16.7616,143.5451,0,0 ; Leviathan"
rx = re.compile(r'([^,;\s]+)')
output = rx.findall(string)
print(output)
Which yields
['417', '364.4265', '2535.2258', '16.7616', '143.5451', '0', '0', 'Leviathan']
Upvotes: 0