Reputation: 59
So I have a txt which has 2 lines as
move 30 20 50
move 60 2 54
and I split those two lines into two elements into a list like this
codereader = 'deneme.txt'
file_handle = open(codereader, "r")
text = file_handle.read().splitlines()
terminal_reader = "{}".format(text)
print(terminal_reader)
and it gives me the solution underneath
#['move 30 20 50','move 60 2 54']
but also I have to put every number to different tuples like 30 and 60 has to go to list called speed. 20 and 2 has to go to list called distance and the last numbers has to go to a list called time. So at the end the solution has to be like this.
speed_values = ['30', '60']
distance_values = ['20','2']
time_values = ['50','54']
but if I add another move into the txt code has to append the last move's numbers into the end of the lists. I dont know how to seperate them individually like this.
Upvotes: 1
Views: 41
Reputation: 6601
You can loop over the values in text
and split each one of them:
line_items = [line.split() for line in text]
Now you have [['move', '30', '20', '50'], ['move', '60', '2', '54']]
.
To group values on the same index you can use the zip
function:
line_items = zip(*[['move', '30', '20', '50'], ['move', '60', '2', '54']])
That will give you an iterator that looks like: [['move', 'move'], ['30', '60'], ...
Now you can assign them into variables.
In [1]: a, b = zip([1, 2], [3, 4])
In [2]: a
Out[2]: (1, 3)
In [3]: b
Out[3]: (2, 4)
Upvotes: 1