title: Reading gzipped text file line-by-line for processing in python 3.2.6
body: <p>I'm a complete newbie when it comes to python, but I've been tasked with trying to get a piece of code running on a machine which has a different version of python (3.2.6) than that which the code was originally built for.</p>
  
  <p>I've come across an issue with reading in a gzipped-text file line-by-line (and processing it depending on the first character). The code (which obviously is written in python > 3.2.6) is</p>
  
  <pre><code>for line in gzip.open(input[0], 'rt'):
      if line[:1] != '&gt;':
          out.write(line)
          continue
  
      chromname = match2chrom(line[1:-1])
      seqname = line[1:].split()[0]
  
      print('&gt;{}'.format(chromname), file=out)
      print('{}\t{}'.format(seqname, chromname), file=mappingout)
  </code></pre>
  
  <p>(for those who know, this strips gzipped FASTA genome files into headers (with ">" at start) and sequences, and processes the lines into two different files depending on this)</p>
  
  <p>I have found <a href="https://bugs.python.org/issue13989" rel="nofollow">https://bugs.python.org/issue13989</a>, which states that mode 'rt' cannot be used for gzip.open in python-3.2 and to use something along the lines of:</p>
  
  <pre><code>import io
  
  with io.TextIOWrapper(gzip.open(input[0], "r")) as fin:
       for line in fin:
           if line[:1] != '&gt;':
               out.write(line)
               continue
  
           chromname = match2chrom(line[1:-1])
           seqname = line[1:].split()[0]
  
           print('&gt;{}'.format(chromname), file=out)
           print('{}\t{}'.format(seqname, chromname), file=mappingout)
  </code></pre>
  
  <p>but the above code does not work:</p>
  
  <pre><code>UnsupportedOperation in line &lt;4&gt; of /path/to/python_file.py:
  read1
  </code></pre>
  
  <p>How can I rewrite this routine to give out exactly what I want - reading the gzip file line-by-line into the variable "line" and processing based on the first character?</p>
  
  <p>EDIT: traceback from the first version of this routine is (python 3.2.6):</p>
  
  <pre><code>Mode rt not supported  
  File "/path/to/python_file.py", line 79, in __process_genome_sequences  
  File "/opt/python-3.2.6/lib/python3.2/gzip.py", line 46, in open  
  File "/opt/python-3.2.6/lib/python3.2/gzip.py", line 157, in __init__
  </code></pre>
  
  <p>Traceback from the second version is:</p>
  
  <pre><code>UnsupportedOperation in line 81 of /path/to/python_file.py:
  read1
  File "/path/to/python_file.py", line 81, in __process_genome_sequences
  </code></pre>
  
  <p>with no further traceback (the extra two lines in the line count are the <code>import io</code> and <code>with io.TextIOWrapper(gzip.open(input[0], "r")) as fin:</code> lines</p>