convert a fasta file to a tab-delimited file using python script
<p>I am student currently learning how to write scripts in python. I have been giving the following exercise. I have to convert a fasta file in the following format: </p>

<pre><code>&gt;header 1 
AATCTGTGTGATAT 
ATATA  
AT
&gt;header 2  
AATCCTCT
</code></pre>

<p>into this: </p>

<pre><code>&gt;header 1  AATCTGTGTGATATATATAAT
&gt;header 2  AATCCTCT 
</code></pre>

<p>I am having some difficulty getting rid of the white space (using line.strip()?) Any help would be very much appreciated...</p>

<p>I am trying to understand the script posted above, line by line. I have posted what I think it is doing below: </p>

<pre><code># open file and iterate through the lines, composing each single line as we go.  
out_lines = []
temp_line = ''
with open('sequences.fa','r') as fp:
     for line in fp:
         if line.startswith('&gt;'):
             out_lines.append(temp_line)
# if line starts with '&gt;', add temp_line to end of out_lines
             temp_line = line.strip() + '\t'
# remove white_space from line and make tab_delimited 
         else:
             temp_line += line.strip()
#  temp_line = temp_line + line.strip - i.e. go to next line after header (sequences) and remove white space. add to temp_line, appended to out_lines.  
with open('output.fa', 'w') as fp_out:
    fp_out.write('\n'.join(out_lines))
# print out_lines, such that out_lines is a single string with space between.  
</code></pre>

python parsing