How to extract and encode data of text file in Python 2.7?
<p>I know it's been asked a lot and I tried some things but I can't make it right:</p>

<p>I have a text file like this:</p>

<pre><code>From: VENCA &lt;email@infoclientes.venca.es&gt;
Subject: =?ISO-8859-1?Q?=BFMaxi,_midi_o_mini=3F_=A1No_pases_d?=
 =?ISO-8859-1?Q?e_largo_porque_esto_te_interesa!?=
Subject: =?UTF-8?Q?Lo_mejor_de_Gmail_est=C3=A9s_donde_est=C3=A9s?=
From: Equipo de Gmail &lt;mail-noreply@google.com&gt;
Subject: =?UTF-8?Q?Tres_consejos_para_sacarle_el_m=C3=A1ximo_partido_a_Gmai?=
From: Equipo de Gmail &lt;mail-noreply@google.com&gt;
Subject: =?UTF-8?Q?Organ=C3=ADzate_mejor_con_la_bandeja_de_entrada_de_Gmail?=
From: Equipo de Gmail &lt;mail-noreply@google.com&gt;
From: VENCA &lt;email@infoclientes.venca.es&gt;
Subject: =?UTF-8?Q?MARINA,_comprueba_que_tus_datos_se?=
 =?UTF-8?Q?an_correctos_y_=C2=A1bienvenid@_a_Venca!?=
Subject: =?UTF-8?Q?Nuevo_inicio_de_sesi=C3=B3n_en_Chrome_con_Windows?=
From: Google &lt;no-reply@accounts.google.com&gt; 
[...]
</code></pre>

<p>Each pair of From/Subject or Subject/From is what I want to extract, in the format:</p>

<pre><code>From: VENCA &lt;email@infoclientes.venca.es&gt; - Subject: Â¿Maxi, midi o mini? Â¡No pases de largo porque esto te interesa!
</code></pre>

<p>[...]</p>

<p>So I have to extract each pair (having in mind that some subjects are 2,3... lines length) gave the format that I want and encode the subject from utf-8, iso... whatever to make them understable </p>

<p>Thanks a lot !</p>

<pre><code>with open('infile.txt') as infile:
    try:
        while True:
            line1 = next(infile).rstrip()
            line2 = next(infile).rstrip()

            if line2.startswith('From:'):
                line1, line2 = line2, line1

            print line1, '-', line2

    except StopIteration:
        pass
</code></pre>

python python-2.7 utf-8 decode encode