Home > Software engineering >  making rows to column and saved in separate files
making rows to column and saved in separate files

Time:01-24

I have 4 text file in a folder and each text file contain many rows of data as follows

cat a.txt
10.0000 0.0000 10.0000 0.0000           
11.0000 0.0000 11.0000 0.0000

cat b.txt
5.1065 3.8423 2.6375 3.5098
4.7873 5.9304 1.9943 4.7599

cat c.txt
3.5257 3.9505 3.8323 4.3359
3.3414 4.0014 4.0383 4.4803

cat d.txt
1.8982 2.0342 1.9963 2.1575
1.8392 2.0504 2.0623 2.2037

I want to make each corresponding rows of the text file to column as

file001.txt

10.0000  5.1065  3.5257  1.8982
 0.0000  3.8423  3.9505  2.0342
10.0000  2.6375  3.8323  1.9963
 0.0000  3.5098  4.3359  2.1575

file002.txt

11.0000  4.7873  3.3414  1.8329
 0.0000  5.9304  4.0014  2.0504
11.0000  1.9943  4.0383  2.0623
 0.0000  4.7599  4.4803  2.2037

Anf finally I want to add this value 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000 to every line so the final output should be

file001.txt

10.0000  5.1065  3.5257  1.8982 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000
 0.0000  3.8423  3.9505  2.0342 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000
10.0000  2.6375  3.8323  1.9963 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000
 0.0000  3.5098  4.3359  2.1575 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000

file002.txt

11.0000  4.7873  3.3414  1.8329 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000
 0.0000  5.9304  4.0014  2.0504 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000
11.0000  1.9943  4.0383  2.0623 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000
 0.0000  4.7599  4.4803  2.2037 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000

Finally, I want to append some comments on the top of every created files

So for example file001.txt should be

# 
# ascertain thin
# Metamorphs
# pch
# what is that
# 5-r
# Add the thing
# liop34
# liop36
# liop45
# liop34
# M(CM)   N(M)   O(S) P(cc)   ab       cd       efgh       ijkl       mnopq    rstuv
#
10.0000  5.1065  3.5257  1.8982 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000
 0.0000  3.8423  3.9505  2.0342 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000
10.0000  2.6375  3.8323  1.9963 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000
 0.0000  3.5098  4.3359  2.1575 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000

CodePudding user response:

files = ["a.txt", "b.txt", "c.txt", "d.txt"]

# get number of columns per file, i.e., 4 in sample data
n_each = np.loadtxt(files[0]).shape[1]

# concatanate transposed data
arrays = np.concatenate([np.loadtxt(file).T for file in files])

# rows are in column now for easier reshaping; reshape and save
n_all = arrays.shape[1]
for n in range(n_all):
    np.savetxt(f"file{str(n 1).zfill(3)}.txt",
               arrays[:, n].reshape(n_each, len(files)).T,
               fmt="%7.4f")

to add a fixed array of values right to the new arrays, you can perform horizontal stacking after tiling the new values n_each times:

# other things same as above
new_values = np.tile([5, 6, 9, 0, 1, 1], (n_each, 1))
for n in range(n_all):
    np.savetxt(f"file{str(n 1).zfill(3)}.txt",
               np.hstack((arrays[:, n].reshape(n_each, len(files)).T,
                          new_values)),
               fmt="%7.4f")

to add comments, header and comments parameters of np.savetxt is useful. we pass the string to header and since it already contains "# " in it, we suppress extra "# " from np.savetxt by passing comments="":

comment = """\
# 
# ascertain thin
# Metamorphs
# pch
# what is that
# 5-r
# Add the thing
# liop34
# liop36
# liop45
# liop34
# M(CM)   N(M)   O(S) P(cc)   ab       cd       efgh       ijkl       mnopq    rstuv
#"""

# rows are in column now for easier reshaping; reshape and save
n_all = arrays.shape[1]
new_values = np.tile([5, 6, 9, 0, 1, 1], (n_each, 1))
for n in range(n_all):
    np.savetxt(f"file{str(n 1).zfill(3)}.txt",
               np.hstack((arrays[:, n].reshape(n_each, len(files)).T,
                          new_values)),
               fmt="%7.4f",
               header=comment,
               comments="")
  • Related