Python Forum
What is the Python Implementation for This?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the Python Implementation for This?
#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?
[Image: AQ0BAxW]
(in case you can't see the image, please click on this link - https://imgur.com/AQ0BAxW
buran write Feb-05-2021, 12:23 PM:
Please, don't post images, copy/paste desired output here
Reply
#2
Read the file line by line.
Separate each line into its components using string.split(' ') - this will be a list (do NOT use string or list as variable names)
Add each member of the list to its respective column list.

Try it, show your work, and post again if you get stuck on any part of this.
Reply
#3
or you can use pandas and read the file into dataframe. Then you can work with columns
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Also posted on Dream.In.Code: https://www.dreamincode.net/forums/topic...n-for-this.
Reply
#5
As mentioned by others, you can do this using pandas transpose() method.
>>> import pandas
>>> file_obj = '''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'''.split('\n')
>>> data = []
>>> for row in file_obj:
...     data.append(row.split())
...
>>> data
[['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']]
>>> df = pandas.DataFrame(data)
>>> df
     0   1   2   3   4   5
0   25  05  38  26  53  04
1   07  45  50  33  19  34
2   55  25  21  30  09  39
3   26  11  30  12  13  41
4   32  23  44  11  50  39
5   45  30  07  44  55  54
6   21  10  35  46  48  27
7   52  41  05  53  11  50
8   40  38  17  43  10  54
9   45  27  29  12  39  31
10  24  42  38  02  18  09
11  13  43  28  06  53  30
12  45  47  29  30  53  13
13  38  45  28  48  47  36
14  25  34  18  06  07  55
>>> rotated = df.transpose()
>>> rotated
   0   1   2   3   4   5   6   7   8   9   10  11  12  13  14
0  25  07  55  26  32  45  21  52  40  45  24  13  45  38  25
1  05  45  25  11  23  30  10  41  38  27  42  43  47  45  34
2  38  50  21  30  44  07  35  05  17  29  38  28  29  28  18
3  26  33  30  12  11  44  46  53  43  12  02  06  30  48  06
4  53  19  09  13  50  55  48  11  10  39  18  53  53  47  07
5  04  34  39  41  39  54  27  50  54  31  09  30  13  36  55
If you'd rather do it manually, that's also fairly easy (pandas DataFrame added to the end, to show they're the same).
>>> file_obj = '''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'''.split('\n')
>>> columns = []
>>> for row in file_obj:
...     values = row.split()
...     for index, val in enumerate(values):
...         if index >= len(columns):
...             columns.append([])
...         columns[index].append(val)
...
>>> columns
[['25', '07', '55', '26', '32', '45', '21', '52', '40', '45', '24', '13', '45', '38', '25'], ['05', '45', '25', '11', '23', '30', '10', '41', '38', '27', '42', '43', '47', '45', '34'], ['38', '50', '21', '30', '44', '07', '35', '05', '17', '29', '38', '28', '29', '28', '18'], ['26', '33', '30', '12', '11', '44', '46', '53', '43', '12', '02', '06', '30', '48', '06'], ['53', '19', '09', '13', '50', '55', '48', '11', '10', '39', '18', '53', '53', '47', '07'], ['04', '34', '39', '41', '39', '54', '27', '50', '54', '31', '09', '30', '13', '36', '55']]
>>> pandas.DataFrame(columns)
   0   1   2   3   4   5   6   7   8   9   10  11  12  13  14
0  25  07  55  26  32  45  21  52  40  45  24  13  45  38  25
1  05  45  25  11  23  30  10  41  38  27  42  43  47  45  34
2  38  50  21  30  44  07  35  05  17  29  38  28  29  28  18
3  26  33  30  12  11  44  46  53  43  12  02  06  30  48  06
4  53  19  09  13  50  55  48  11  10  39  18  53  53  47  07
5  04  34  39  41  39  54  27  50  54  31  09  30  13  36  55
Reply
#6
Just to mention that reading into pandas can be

import pandas as pd 

df = pd.read_csv('test.txt', sep = ' ', header=None)
print(df)
print(df.dtypes)
Output:
0 1 2 3 4 5 0 25 5 38 26 53 4 1 7 45 50 33 19 34 2 55 25 21 30 9 39 3 26 11 30 12 13 41 4 32 23 44 11 50 39 5 45 30 7 44 55 54 6 21 10 35 46 48 27 7 52 41 5 53 11 50 8 40 38 17 43 10 54 9 45 27 29 12 39 31 10 24 42 38 2 18 9 11 13 43 28 6 53 30 12 45 47 29 30 53 13 13 38 45 28 48 47 36 14 25 34 18 6 7 55 0 int64 1 int64 2 int64 3 int64 4 int64 5 int64 dtype: object
nilamo likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python implementation of Longest Common Substring problem Bolt 0 570 Sep-17-2023, 08:31 PM
Last Post: Bolt
  Python Turtle and order of implementation query Parsleigh 2 2,782 Mar-04-2019, 02:43 PM
Last Post: Parsleigh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020