Posts: 6
Threads: 2
Joined: Jun 2021
Jun-27-2021, 04:18 PM
(This post was last modified: Jun-27-2021, 04:18 PM by mesh01.)
Hi,
I have an "input.txt" file which contains this column of numbers:
I would like that column to be divided into columns side by side, whose the number of elements is established by this vector: 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.
Posts: 12,028
Threads: 485
Joined: Sep 2016
Posts: 6
Threads: 2
Joined: Jun 2021
Jun-28-2021, 10:00 PM
(This post was last modified: Jun-28-2021, 10:00 PM by mesh01.)
(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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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.
Posts: 1,583
Threads: 3
Joined: Mar 2020
Instead of writing the columns to a file, collect each into a variable (or elements of a list). Then combine them with zip_longest().
Posts: 6
Threads: 2
Joined: Jun 2021
Jun-29-2021, 01:35 PM
(This post was last modified: Jun-29-2021, 01:35 PM by mesh01.)
This is better already:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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.
Posts: 6
Threads: 2
Joined: Jun 2021
(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.
Posts: 6,783
Threads: 20
Joined: Feb 2020
Jul-02-2021, 04:52 PM
(This post was last modified: Jul-02-2021, 04:52 PM by deanhystad.)
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.
1 2 3 4 5 6 7 8 9 |
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))
|
Posts: 1,583
Threads: 3
Joined: Mar 2020
zip_longest might be useful as well. Lets you not worry about the number of columns.
1 2 3 4 5 6 7 8 |
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))
|
Posts: 1,950
Threads: 8
Joined: Jun 2018
Writing to file in 'table-style' (4 space columns, right-justified) can be done with something like this:
1 2 3 |
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.
|