1

when retrieving a file to save it from gmail, I get the following error message:

Traceback (most recent call last):
  File "C:\a.py", line 32, in on_hello
    if getmail(self):
  File "C:\a.py", line 96, in getmail
    fp = open(att_path, 'wb')
IOError: [Errno 22] invalid mode ('wb') or filename: '.\\=?iso-8859-1?Q?CC_GM=5F
Est=E1vel.xlsx?='

Now, I suspect that =?iso-8859-1?Q?CC_GM=5FEst=E1vel.xlsx?= is the problem, how can I convert it to ansii? I tried using all sorts of .decode and .encode combinations with no success.

Thanks

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
relima
  • 3,462
  • 5
  • 34
  • 53

3 Answers3

3

You are looking at a Quoted-printable encoding (a variant as used in MIME email headers, called encoded-word). The email.header module handles this for you:

>>> from email.header import decode_header
>>> for part in decode_header('=?iso-8859-1?Q?CC_GM=5FEst=E1vel.xlsx?='):
...     value = str(*part)
...     print(value)
...
CC GM_Estável.xlsx
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

That's a MIME-encoded email header.

You should* be able to use something in here, perhaps decode:

http://docs.python.org/2/library/email.header.html

*I don't regularly write Python; your mileage may vary.

Joe
  • 41,484
  • 20
  • 104
  • 125
0

First of all, iso-8859-1 has a number of characters that ansi does not have see this. Are you sure that you can safely ignore all those characters in your situation? One other idea which I would tend more towards would be an iso-8859-1, to (some flavour of Unicode, i.e. utf8, utf16, etc). Here is an answer to your question from [SO] which will do the conversion2. CHEERS!

Community
  • 1
  • 1
happy coder
  • 1,517
  • 1
  • 14
  • 29