Python: Help with a new line character

I read in a tab delimited text file, then I have a list for each line, then I index the first record of each list, then I write it to a file. code below:

import csv z = csv.reader(open('output.blast'), delimiter='\t') k = open('output.fasta', 'w') for row in z: print row[1:12] for i in row[1:12]: k.write(i+'\t') 

When writing to a file, it writes it as one long line, I would like a new line to start after the 11th (last) record in each list. But I can’t figure out where to put the new charater line

+4
source share
3 answers

It sounds like you just want it after each line, so put it at the end of the for loop, which iterates through the lines:

 for row in z: print row[1:12] for i in row[1:12]: k.write(i+'\t') k.write('\n') 
+6
source

If you write it back to a tab-separated format, why not use the csv package again?

 r = csv.reader(open('output.blast'),delimiter='\t') outfile = open('output.fasta','w') w = csv.writer(outfile,delimiter='\t') w.writerows(row[1:12] for row in r) outfile.flush() outfile.close() 
+2
source
 import csv z = csv.reader(open('output.blast'), delimiter='\t') k = open('output.fasta', 'w') for row in z: print row[1:12] for i in row[1:12]: k.write(i+'\t') k.write('\n ') # <- write out the newline here. 
0
source

Source: https://habr.com/ru/post/1314472/


All Articles