Extracting selected columns from a datafile using python
Question: I have a data file like this

    
    
    0.000       1.185e-01  1.185e-01  3.660e-02  2.962e-02  0.000e+00  0.000e+00  0.000e+00  0.000e+00  0.000e+00
    0.001       1.185e-01  1.185e-01  3.660e-02  2.962e-02  -1.534e-02  -1.534e-02  8.000e-31  8.000e-31  0.000e+00
    0.002       1.185e-01  1.185e-01  3.659e-02  2.961e-02  -1.541e-02  -1.541e-02  -6.163e-01  -6.163e-01  -4.284e-05
    0.003       1.186e-01  1.186e-01  3.657e-02  2.959e-02  -1.547e-02  -1.547e-02  -8.000e-31  -8.000e-31  0.000e+00
    0.004       1.186e-01  1.186e-01  3.657e-02  2.959e-02  -2.005e-32  -2.005e-32  -8.000e-31  -8.000e-31  0.000e+00
    0.005       1.186e-01  1.186e-01  3.657e-02  2.959e-02  -2.005e-32  -2.005e-32  -8.000e-31  -8.000e-31  0.000e+00
    0.006       1.187e-01  1.186e-01  3.657e-02  2.959e-02  -2.005e-32  -2.005e-32  -8.000e-31  -8.000e-31  0.000e+00
    0.007       1.187e-01  1.187e-01  3.657e-02  2.959e-02  -2.005e-32  -2.005e-32  -8.000e-31  -8.000e-31  0.000e+00
    0.008       1.188e-01  1.187e-01  3.657e-02  2.959e-02  -2.005e-32  -2.005e-32  -8.000e-31  -8.000e-31  0.000e+00
    0.009       1.188e-01  1.187e-01  3.657e-02  2.959e-02  -2.005e-32  -2.005e-32  -8.000e-31  -8.000e-31  0.000e+00
    

I want to copy only selected columns from this file to another file. Suppose I
copy the 1st, 2nd and 6th columns to a file, then that file should look like

    
    
    0.000       1.185e-01  0.000e+00
    0.001       1.185e-01  -1.534e-02
    0.002       1.185e-01  -1.541e-02
    0.003       1.186e-01  -1.547e-02
    0.004       1.186e-01  -2.005e-32
    0.005       1.186e-01  -2.005e-32
    0.006       1.187e-01  -2.005e-32
    0.007       1.187e-01  -2.005e-32
    0.008       1.188e-01  -2.005e-32
    0.009       1.188e-01  -2.005e-32
    

This is a very large formatted text file which was initially written like this

    
    
    f=open('myMD.dat','w')
    s='%8.3e  %8.3e  %8.3e  %8.3e  %8.3e  %8.3e  %8.3e  %8.3e  %8.3e\t\t'%(xpos1[i],ypos1[i],xvel1[i],yvel1[i],xacc1[i],yacc1[i],xforc[i],yforc[i],potn[i])
    f.write(s)
    f.close()
    

I am programming in python. How can I do this?


Answer: This will read a given input file and select rows using a given comma
separated list of rows:

    
    
    import sys
    input_name = sys.argv[1]
    column_list = [(int(x) - 1) for x in sys.argv[2].split(',')]
    with open(input_name) as input_file:
        for line in input_file:
            row = line.split()
            for col in column_list:
                print row[col],
            print ""
    

It reads and prints one line at a time, which means it should be able to
handle an arbitrarily large input file. Using your example data as
`input.txt`, I ran it like this:

    
    
    python selected_columns.py input.txt 1,2,6
    

It produced the following output (ellipsis used to show lines removed for
brevity):

    
    
    0.000 1.185e-01 0.000e+00 
    0.001 1.185e-01 -1.534e-02 
    ...
    0.009 1.188e-01 -2.005e-32 
    

You can save the output to a file using redirection:

    
    
    python selected_columns.py input.txt 1,2,6 > output.txt
    

