ASCII encoding error: ordinal not in range(128)
jicman
cabrera at wrc.xerox.com
Thu Mar 22 16:40:04 EST 2001
fredrik at pythonware.com says...
>summary: someone's handing you unicode strings (probably
>the COM interface), and you're trying to use them as 8-bit
>strings (probably by passing them to a method that expects
>8-bit data) without specifying an encoding.
>
>(if you want me to be more precise, you have to show us
>more code).
def FindIfAttachments(myMsg,spar,wID,fam):
myAtt = myMsg.Attachments
if myAtt.Count > 0:
LogDir = os.path.join(logspath,'Attachments',spar)
if os.path.isdir(LogDir):
#do nothing for now
nothing = 1
else:
os.makedirs(LogDir)
i = 1
while i <= myAtt.Count:
if myAtt.Item(i).Type == 1:
code = myAtt.Item(i).DisplayName
name = code.encode()
if name == '':
name = 'Untitled Attachment.msg'
else:
name = CleanNameToSave(name)
name = spar + ' attachment - ' + name
name = os.path.join(LogDir,name)
error = myAtt.Item(i).SaveAsFile(name)
i = i + 1
myMsg is a COM pointer to an Outlook 2000 message. myAtt is the pointer to the
attachments. DisplayName is the name of the attachment causes the problem.
Here is the error message:
C:\>qna0.py
Traceback (most recent call last):
File "C:\myprograms\qna0.py", line 1570, in ?
main()
File "C:\myprograms\qna0.py", line 1556, in main
ProcessAllItems(wf2,workpath,workpath,NSpace)
File "C:\myprograms\qna0.py", line 1484, in ProcessAllItems
FindIfAttachments(em,Rec['SPAR'],wID,fam)
File "C:\myprograms\qna0.py", line 1156, in FindIfAttachments
name = code.encode()
UnicodeError: ASCII encoding error: ordinal not in range(128)
>
>to solve this, you have to add an explicit conversion to the
>right place. e.g:
>
> s = u.encode("latin-1")
>
>where "u" is a unicode string and "s" an 8-bit ISO Latin string.
>
>(if you don't do this, Python will convert the string anyway when
>you pass it to an 8-bit function. the default conversion assumes
>ASCII, which is why you get that exception)
So, if I am understanding this right, the line
name = code.encode()
should be
name = code.encode('latin-1')
or perhaps
name = code.encode('PC-8')
Thanks very much.
I'll try this and let you know....
thank you so very much
josé
More information about the Python-list
mailing list