pythonflea
(Fudgie Braun)
1
I have a Windows text file with the following info inside it:
25 05 38 26 53 04
07 45 50 33 19 34
55 25 21 30 09 39
26 11 30 12 13 41
32 23 44 11 50 39
45 30 07 44 55 54
21 10 35 46 48 27
52 41 05 53 11 50
40 38 17 43 10 54
45 27 29 12 39 31
24 42 38 02 18 09
13 43 28 06 53 30
45 47 29 30 53 13
38 45 28 48 47 36
25 34 18 06 07 55
How can I code them to break that info apart into six columns and put each column into their own array? (Like in the image in this link - arrays)
Kindly include pseudo-code please - so as to guide me at least…
You can read the file one line at a time like this:
with open('path/to/file.txt', 'r') as f:
for line in f:
pass
You can split each line into words like this:
line = '12 13 14 15 16'
words = line.split()
Don’t forget to convert each word (a string) into a number using the
int
function.
Now you need six lists, one for each column:
columns = [[] for i in range(6)]
Once you have your six columns, you can place each word into the
appropriate column. Here are two ways to do it, in pseudo-code:
loop with i from 0 to 5:
take word i and append it to column i
Alternatively:
loop over the zip of columns and words:
append the word to the column
(Hint: there’s a function zip
that you can use.)
Try writing your own code from those hints. If you need further help,
show us how far you got.
Good luck!