Reputation: 352
I have a string input like this:
1,2000,5,1;1,2050,5,2;2,3000,10,3
How can I split it into a list of lists like this:
[ [1, 2000, 5, 1], [1, 2050, 5, 2], [2, 3000, 10, 3], ...]
I tried regex but kept getting confused between both commas and semicolons.
Upvotes: 0
Views: 1815
Reputation: 57033
Just do exactly as you said: split by semicolons and then by commas.
s = "1,2000,5,1;1,2050,5,2;2,3000,10,3"
[x.split(',') for x in s.split(';')]
#[['1', '2000', '5', '1'], ['1', '2050', '5', '2'], ['2', '3000', '10', '3']]
If you further want lists of numbers, convert the strings to numbers:
[list(map(int, x.split(','))) for x in s.split(';')]
#[[1, 2000, 5, 1], [1, 2050, 5, 2], [2, 3000, 10, 3]]
Upvotes: 4