Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: HeaderParseError Followup-To: comp.lang.python Date: Mon, 04 Jul 2011 11:51:41 +0200 Organization: None Lines: 35 Message-ID: References: <97dc37F7gaU1@mid.individual.net> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1309773097 449 eJwFwYEBACAEBMCVJJ7G4cv+I3TnGwsMg8N8fKjaMKWxR6oPeyEf7T7V6JSuqAxS9NQ+cz8wCBHi (4 Jul 2011 09:51:37 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Mon, 4 Jul 2011 09:51:37 +0000 (UTC) X-User-ID: eJwNwgkRADEIBDBLvAvIKbT4l3A3iSsYEwaH+f6mVFetMvGG5+iBbNZtk3ohoEjyAy5ZoVUSj6vs3sYU3R8zzhRF Cancel-Lock: sha1:Xf1i0rQGntwcA3QoMhw6/bKhUqE= X-NNTP-Posting-Host: eJwNyscBwDAIBLCVKEcbB2Oz/wiJ3jJ19gm4OWxtQ+k1bzFoglZALxxafP9Tk+gnxHIfq1deFshsDu3pcb5J2iOpYZpwyPkAmH8Ylg== Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:8765 Thomas Guettler wrote: > I get a HeaderParseError during decode_header(), but Thunderbird can > display the name. > >>>> from email.header import decode_header >>>> decode_header('=?iso-8859-1?B?QW5tZWxkdW5nIE5ldHphbnNjaGx1c3MgU_xkcmluZzNwLmpwZw==?=') > Traceback (most recent call last): > File "", line 1, in > File "/usr/lib64/python2.6/email/header.py", line 101, in decode_header > raise HeaderParseError > email.errors.HeaderParseError > > > How can I parse this in Python? Trying to decode as much as possible: >>> s = "QW5tZWxkdW5nIE5ldHphbnNjaGx1c3MgU_xkcmluZzNwLmpwZw==?=" >>> for n in range(len(s), 0, -1): ... try: t = s[:n].decode("base64") ... except: pass ... else: break ... >>> n, t (49, 'Anmeldung Netzanschluss S\x19\x1c\x9a[\x99\xcc\xdc\x0b\x9a\x9c\x19') >>> print t.decode("iso-8859-1") Anmeldung Netzanschluss S[ÌÜ >>> s[n:] 'w==?=' The characters after "...Netzanschluss " look like garbage. What does Thunderbird display?