I'm trying to decode email Subject headers.
I'm doing this (the regex is for adding a space between the two = 's:
header = '=?iso-8859-1?B?TU9UT1IubmwgbmlldXdzYnJpZWYgPiBOaWV1d2UgdmVya29vcHRvcHBl?==?iso-8859-1?B?ciBTdXp1a2kg?='
header = re.sub(r"(==)(?!$)", u"\0= =", header)
email.header.decode_header(header)
But that throws an HeaderParseError:
HeaderParseError Traceback (most recent call last)
/home/leon/<ipython console> in <module>()
/usr/lib/python2.7/email/header.pyc in decode_header(header)
106 # now we throw the lower level exception away but
107 # when/if we get exception chaining, we'll preserve it.
--> 108 raise HeaderParseError
109 if dec is None:
110 dec = encoded
The funny thing is, if I copy the output of 开发者_JAVA百科the re.sub() to my clipboard and do:
email.header.decode_header('=?iso-8859-1?B?TU9UT1IubmwgbmlldXdzYnJpZWYgPiBOaWV1d2UgdmVya29vcHRvcHBl?= =?iso-8859-1?B?ciBTdXp1a2kg?=')
it works!
So I guess something's wrong with the encoding of re.sub() but I don't know how to fix this.
You lack a space between the RFC2047 tokens in the example which doesn't work. Your attempt to repair it is, however, also incorrect; you should be replacing with u"= ="
, not u"\0= ="
.
It would be much better if you could find the source of such errors and correct it, rather than attempt to fix it up afterwards based on, at best, good guesses about what your data ought to be.
精彩评论