[Email-SIG] email.header.decode_header eats my spaces

Jasper Spaans jasper at vs19.net
Tue Mar 27 16:07:51 CEST 2007


Op 27-mrt-2007, om 2:40 heeft Tokio Kikuchi het volgende geschreven:

> What program make this output ?
>
> Python 2.5 (r25:51908, Feb  7 2007, 19:53:49)
> [GCC 3.3.5 (Debian 1:3.3.5-13)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import email.header
> >>> t = email.header.decode_header('=?utf-8?B?UmVuw6k=?= Pfeiffer <>')
> >>> t
> [('Ren\xc3\xa9', 'utf-8'), ('Pfeiffer <>', None)]
> >>> h = email.header.make_header(t)
> >>> unicode(h)
> u'Ren\xe9 Pfeiffer <>'
> >>> unicode(h).encode('iso-8859-1')
> 'Ren\xe9 Pfeiffer <>'
>
> Use email.header module to re-construct your header from the  
> decoded tuple list.

Hmm - that looks like a better solution, I was using the following  
(part is an instance of email.Message.Message):

         (from_name, from_addr) = parseaddr(part.get('from'))
         f = ''
         for (piece, charset) in decode_header(from_name):
             if charset:
                 f += piece.decode(charset, 'replace')
             else:
                 f += piece

         from_name = f
         print "%s <%s> vs %s" % (from_name, from_addr, part.get 
('from'))

which feels like reinventing wheels.. (I was expecting a helper  
function to do all of this in the email.header package, and unicode 
(part.get('from')) can't work..)

Jasper
-- 
Jasper Spaans                                       http://jsp.vs19.net/
                This line was last modified 0 seconds ago.





More information about the Email-SIG mailing list