[Python-Dev] Adding .decode() method to Unicode
Martin v. Loewis
martin@loewis.home.cs.tu-berlin.de
Tue, 12 Jun 2001 20:08:31 +0200
> Having just followed this thread tangentially, I do have to say it
> seems quite cool to be able to do something like the following in
> Python 2.2:
>
> >>> s = msg['from']
> >>> parts = s.split('?')
> >>> if parts[2].lower() == 'q':
> ... name = parts[3].decode('quopri')
> ... elif parts[2].lower() == 'b':
> ... name = parts[3].decode('base64')
> ...
What is the type of parts[3] here? If it is a plain string, it is
already possible:
>>> 'SGVsbG8=\n'.decode("base64")
'Hello'
I doubt you'd ever have a Unicode string that represents a
base64-encoded byte string, and if you had, .decode would probably do
the wrong thing:
>>> import codecs
>>> enc,dec,_,_ = codecs.lookup("base64")
>>> dec(u'SGVsbG8=\n')
('Hello', 9)
Note that this returns a byte string, not a Unicode string.
Regards,
Martin