[Tutor] COnvert a string?

Magnus Lyckå magnus@thinkware.se
Sun Jul 6 08:40:02 2003


At 14:03 2003-07-06 +0200, j2 wrote:
>Aha, still doesnt work tho. Hjälp? :)

The code below prints the right thing for me. The last conversion
to cp850 is there to make the Swedish letters look right at a
Windows command prompt, since that still uses an ancient DOS encoding.
You don't want that if you print in a normal Windows context or run
it in a better operating system. I'm using Python 2.2.2.

--------
from email.Header import decode_header

raw_text = 
"""=?iso-8859-1?Q?Hej=2C_tack_f=F6r_bra_team_work_p=E5_ndc5_i_fredags_efterm?=
      =?iso-8859-1?Q?iddag!_/Rolf?="""

header = decode_header(raw_text)

result = u''

for text, encoding in header:
     result += text.decode(encoding)

print result.encode('cp850')
--------

As you see I'm not using the safety nets that Michal showed. They
are not needed for this particular example, but it's probably good
to have something like that. I think it's enough with just the try
block though, it will catch empty encodings, and in general I try
to avoid unqualified except clauses. I guess I'd use (untested):

for text, encoding in header:
     try:
         result += text.decode(encoding)
     except (UnicodeError, ValueError):
         result += text



--
Magnus Lycka (It's really Lyckå), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The Agile Programming Language