[Tutor] More string conversions.

j2 spamfilter2@mupp.net
Sun Jul 6 15:47:01 2003


This is a multi-part message in MIME format.

------=_NextPart_000_0C8E_01C34407.B9FE5AA0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Thankyou all for the help with decoding the email headers.. Now i got =
myself another problem.

I am trying to do=20

#!/usr/bin/python2.2
import email
import email.Header

x =3D =
"=3D?iso-8859-1?Q?Hej=3D2C_tack_f=3DF6r_bra_team_work_p=3DE5_ndc5_i_freda=
gs_efterm?=3D=3D?iso-8859-1?Q?iddag!_/Rolf?=3D"

y =3D email.Header.decode_header(x)
s =3D u''
for text, charset in header_line:
        if charset:
                try:
                        s +=3D text.decode(charset)
                except:
                        s +=3D text
        else:
                s +=3D text

print y
print s

cookiemonster:~/scripts# ./test.py  =20
[('Hej, tack f\xf6r bra team work p\xe5 ndc5 i fredags eftermiddag! =
/Rolf', 'iso-8859-1')]

Traceback (most recent call last):
  File "./test.py", line 28, in ?
    print s
UnicodeError: ASCII encoding error: ordinal not in range(128)
cookiemonster:~/scripts#=20

But how could i convert the text in "y" to "plain ascii"?  
------=_NextPart_000_0C8E_01C34407.B9FE5AA0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1170" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Thankyou all for the help with decoding =
the email=20
headers.. Now i got myself another problem.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>I am trying to do </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>#!/usr/bin/python2.2<BR>import =
email<BR>import=20
email.Header</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>x =3D=20
"=3D?iso-8859-1?Q?Hej=3D2C_tack_f=3DF6r_bra_team_work_p=3DE5_ndc5_i_freda=
gs_efterm?=3D=3D?iso-8859-1?Q?iddag!_/Rolf?=3D"</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>y =3D =
email.Header.decode_header(x)<BR>s =3D u''<BR>for=20
text, charset in =
header_line:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if=20
charset:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
try:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
=20
s +=3D=20
text.decode(charset)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
except:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;=20
s +=3D text<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
else:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;=20
s +=3D text<BR></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>print y<BR>print s</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>cookiemonster:~/scripts# =
./test.py&nbsp;&nbsp;=20
<BR>[('Hej, tack f\xf6r bra team work p\xe5 ndc5 i fredags eftermiddag! =
/Rolf',=20
'iso-8859-1')]</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Traceback (most recent call =
last):<BR>&nbsp; File=20
"./test.py", line 28, in ?<BR>&nbsp;&nbsp;&nbsp; print =
s<BR>UnicodeError: ASCII=20
encoding error: ordinal not in range(128)<BR>cookiemonster:~/scripts#=20
</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>But how could i convert the text in "y" =
to "plain=20
ascii"?&nbsp; </DIV></FONT></BODY></HTML>

------=_NextPart_000_0C8E_01C34407.B9FE5AA0--