Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Subdividing a column
#1
Hi,

I have an "input.txt" file which contains this column of numbers:

5 
77 
1
-4
9
17
13
2
1
5
63
8
I would like that column to be divided into columns side by side, whose the number of elements is established by this vector:
(4,2,5,1)
Output should look like this:

Output:
5 9 13 8 77 17 2 1 1 -4 5 63
The output should be saved in an "output.txt" file.

Any tips?

Thank you,

m.
Reply
#2
what have you tried?
Reply
#3
(Jun-28-2021, 02:25 AM)Larz60+ Wrote: what have you tried?

EDIT
For now I can only read the column of numbers from "input.txt" file and write 4 files for the 4 subdivisions:

input.txt file:
5 
77 
1
-4
9
17
13
2
1
5
63
8
#python3.6
import numpy as np   

d = np.loadtxt('input.txt')

A= 4
B= 2
C= 5
D= 1

with open('output1.txt', 'w') as f_out:
    for i in range(0,A):
        f_out.write(f"{d[i]}\n")
         
with open('output2.txt', 'w') as f_out:   
    for i in range(A,A+B):
        f_out.write(f"{d[i]}\n")

with open('output3.txt', 'w') as f_out:
    for i in range(A+B,A+B+C):
        f_out.write(f"{d[i]}\n")
         
with open('output4.txt', 'w') as f_out:   
    for i in range(A+B+C,A+B+C+D):
        f_out.write(f"{d[i]}\n")
output1.txt file:
Output:
5 77 1 -4
output2.txt file:
Output:
9 17
output3.txt file:
Output:
13 2 1 5 63
output4.txt file:
Output:
8
But I'm trying to write only one text file with 4 columns.

Bye,

m.
Reply
#4
Instead of writing the columns to a file, collect each into a variable (or elements of a list). Then combine them with zip_longest().
Reply
#5
This is better already:

import numpy as np

data = (np.loadtxt("./input.txt", dtype="int"))

A= 4
B= 2
C= 5
D= 1

x = np.array(data[0:A])
y = np.array(data[A:A+B])
z = np.array(data[A+B:A+B+C])
w = np.array(data[A+B+C:A+B+C+D])

print(x,y,z,w)

with open('output.txt','w') as f_out:

    f_out.write(f"{x}\n{y}\n{z}\n{w}\n")
file.txt output:
Output:
[ 5 77 1 -4] [ 9 17] [13 2 1 5 63] [8]
Now I have to figure out how to get the values ​​written in column (and not in line) and without the square brackets.

Bye,

m.
Reply
#6
(Jun-29-2021, 01:35 PM)mesh01 Wrote: Now I have to figure out how to get the values ​​written in column (and not in line) and without the square brackets.

Bye,

m.
Nobody has a solution?
(I also tried with the zip_longest () function).
Maybe I asked a too trivial question?

m.
Reply
#7
Not too simple, just odd. Ragged rows in a file is not that uncommon, but ragged columns is unusual. I don't see how this can be useful, but I think the code below produces the results you are asking for.
from itertools import islice

data = iter([5, 77, 1, -4, 9, 17, 13, 2, 1, 5, 63, 8])
slices = [4, 2, 4, 1]
columns = [list(islice(data, slice)) for slice in slices]

for r in range(max(slices)):
    row = ['' if r >= len(column) else f'{column[r]}' for column in columns]
    print('\t'.join(row))
Reply
#8
zip_longest might be useful as well. Lets you not worry about the number of columns.

from itertools import islice, zip_longest

data = iter([5, 77, 1, -4, 9, 17, 13, 2, 1, 5, 63, 8])
slices = [4, 2, 4, 1]
columns = (islice(data, slice) for slice in slices)

for row in zip_longest(*columns, fillvalue=""):
    print('\t'.join(str(x) for x in row))
Reply
#9
Writing to file in 'table-style' (4 space columns, right-justified) can be done with something like this:

with open('output.txt', 'w') as f:
    for row in zip_longest(*columns, fillvalue=""):
        print(*(item.rjust(4, ' ') for item in row), file=f)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

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