python - Read in a file, splitting and then writing out desired output -


i new python, , having problems can't seem find answers to. have large file trying read in , split , write out specific information. having trouble read in , split, printing same thing on , on again.

blast_output = open("blast.txt").read() line in blast_output:     subfields = [item.split('|') item in blast_output.split()]     print(str(subfields[0][0]) + "\t" + str(subfields[0][1]) + "\t" + str(subfields[1][3]) + "\t" + str(subfields[2][0])) 

my input file has many rows this:

c0_g1_i1|m.1    gi|74665200|sp|q9hgp0.1|pvg4_schpo      100.00  372     0       0       1       372     1       372     0.0       754 c1002_g1_i1|m.801       gi|1723464|sp|q10302.1|yd49_schpo       100.00  646     0       0       1       646     1       646     0.0      1310 c1003_g1_i1|m.803       gi|74631197|sp|q6bdr8.1|nse4_schpo      100.00  246     0       0       1       246     1       246     1e-179    502 c1004_g1_i1|m.804       gi|74676184|sp|o94325.1|pex5_schpo      100.00  598     0       0       1       598     1       598     0.0      1227 

the output receiving this:

c0_g1_i1    m.1 q9hgp0.1    100.00 c0_g1_i1    m.1 q9hgp0.1    100.00 c0_g1_i1    m.1 q9hgp0.1    100.00 c0_g1_i1    m.1 q9hgp0.1    100.00 

but wanting is

c0_g1_i1      m.1    q9hgp0.1    100.0 c1002_g1_i1   m.801  q10302.1    100.0 c1003_g1_i1   m.803  q6bdr8.1    100.0 c1004_g1_i1   m.804  o94325.1    100.0 

you don't need call read method of file object, iterate on it, line line. replace blast_output line in loop avoid repeating same action across iterations:

with open("blast.txt") blast_output:     line in blast_output:         subfields = [item.split('|') item in line.split()]         print("{:15}{:10}{:10}{:10}".format(subfields[0][0], subfields[0][1],                                              subfields[0][1], subfields[1][3], subfields[2][0])) 

i have opened file in context using with, closing automatically done python. have used string formatting build final string.


c0_g1_i1       m.1       m.1       q9hgp0.1   c1002_g1_i1    m.801     m.801     q10302.1   c1003_g1_i1    m.803     m.803     q6bdr8.1   c1004_g1_i1    m.804     m.804     o94325.1   

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -