jeudi 5 janvier 2017

How to generate repeated header patten by reading header-input from a file?

Suppose I have a file that has the following unique headerline.

block values Alfa Beta Gamma

I want to create a table-header after reading this file. **The problem is that the number of columns starting at Alfa represents the sample. So, there may be any number of samples when reading different files of this kind. **

The other files could have header as:

block values Alfa Beta Gamma Delta Theta

with the following script:

my_file = open('data.txt', 'r')
for lines in my_file.read().split('\n'):
    header = lines.split('\t')
    sample = header[2::]
    with open("new_file.txt", "w") as output:
        output.write("block\t" + '\t'.join(sample))
        output.close()

When I have header-input as:

block values Alfa Beta Gamma

I get the header-output as:

block Alfa Beta Gamma

But, I want my output header to be:

block values Alfa_a Alfa_b Beta_a Beta_b Gamma_a Gamma_b

So/And, if there are 5 samples in the header in any other input-file, I want the header-output to be:

Header-input:

block values Alfa Beta Gamma Delta Theta

Header-output:

block values Alfa_a Alfa_b Beta_a Beta_b Gamma_a Gamma_b Delta_a Delta_b Theta_a Theta_b

So, the script should be able to generate the header in this format (pattern) for each samples in the input file.

Any other solutions appreciated.

Thanks,

Aucun commentaire:

Enregistrer un commentaire