解码UTF-8编码的CSV文件时出错
第一次在这里发帖:)
我在尝试从电子邮件收件箱下载所有附件时遇到问题。 我下载它们,然后将它们写入一个文件,并指定其路径。 它非常适合直接下载到文件的 .png 文件,但当涉及 .csv 文件时,它会给出以下错误消息:
OSError: [Errno 22] Invalid argument: 'C:\Users\antoi\OneDrive\Bureau\python_secge\=?UTF-8?B?RXh0cmFjdCBzZXggZ8OpIHB1YmxpYy0yMDIyLTAzLTI2LTAwLTAwLTI2LmNzdg==?='
我认为它不能很好地解码 csv 文件的名称,但我不'不知道为什么。
感谢您的帮助!
如果您想查看下面的代码:
import smtplib
import imaplib
import base64
import os
import email
smtp_address = 'smtp.gmail.com'
smtp_port = 465
email_user = 'XXXX'
email_pass = 'XXXXX'
mail = imaplib.IMAP4_SSL('imap.gmail.com',993)
mail.login(email_user, email_pass)
mail.select('Inbox')
type, data = mail.search(None, 'ALL')
mail_ids=data[0]
idlist=mail_ids.split()
for num in data[0].split():
typ, data = mail.fetch(num, '(RFC822)' )
raw_email = data[0][1]
# converts byte literal to string removing b''
raw_email_string = raw_email.decode('utf-8')
email_message = email.message_from_string(raw_email_string)
# downloading attachments
for part in email_message.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
fileName = part.get_filename()
if bool(fileName):
filePath = os.path.join(r'C:\Users\antoi\OneDrive\Bureau\python_secge', fileName)
if not os.path.isfile(filePath) :
fp = open(filePath, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
subject = str(email_message).split("Subject: ", 1)
我尝试更改 csv 文件的名称,该文件下载良好,但其内容就像未解码一样:
# #*%%*525EE\ÿ ¿ A" ÿÄ7 ÿÚ iÙWúßóÓ ÅIq«‚ÙÊŸ§ˆ˜²‚6`ض p²#áíîŸÐà ¼ïDù÷.ŽéCÅ >ªþ®|…dÕË' <å8
!õÑàäH¬
first time posting here :)
I have an issue while trying to download all the attachments from an email Inbox.
I download them, then write them into a file, which I specify the path to.
It works perfectly well for .png files, which are directly dowloaded to the file, but when it comes to a .csv file, it gives me this error message :
OSError: [Errno 22] Invalid argument: 'C:\Users\antoi\OneDrive\Bureau\python_secge\=?UTF-8?B?RXh0cmFjdCBzZXggZ8OpIHB1YmxpYy0yMDIyLTAzLTI2LTAwLTAwLTI2LmNzdg==?='
I think it does not decode well the name of the csv file, but I don't know why.
Thanks for your help!
If you want to look at my code below :
import smtplib
import imaplib
import base64
import os
import email
smtp_address = 'smtp.gmail.com'
smtp_port = 465
email_user = 'XXXX'
email_pass = 'XXXXX'
mail = imaplib.IMAP4_SSL('imap.gmail.com',993)
mail.login(email_user, email_pass)
mail.select('Inbox')
type, data = mail.search(None, 'ALL')
mail_ids=data[0]
idlist=mail_ids.split()
for num in data[0].split():
typ, data = mail.fetch(num, '(RFC822)' )
raw_email = data[0][1]
# converts byte literal to string removing b''
raw_email_string = raw_email.decode('utf-8')
email_message = email.message_from_string(raw_email_string)
# downloading attachments
for part in email_message.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
fileName = part.get_filename()
if bool(fileName):
filePath = os.path.join(r'C:\Users\antoi\OneDrive\Bureau\python_secge', fileName)
if not os.path.isfile(filePath) :
fp = open(filePath, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
subject = str(email_message).split("Subject: ", 1)
I tried to change the name of the csv file, which downloaded well, but the content of it was as if it was not decoded :
# #*%%*525EE\ÿ ¿ A" ÿÄ7 ÿÚ iÙWúßóÓ ÅIq«‚ÙÊŸ§ˆ˜²‚6`ض p²#áíîŸÐà ¼ïDù÷.ŽéCÅ >ªþ®|…dÕË' <å8
!õÑàäH¬
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
https://dmorgan.info/posts/encoded-word-syntax/”中被盗。
从
1 和消除语法:“ IS”具有字面意思。您的意思是“ ==”?
Stolen from Encoded-word Syntax1:
Apply as follows:
1 and eliminated SyntaxWarning: "is" with a literal. Did you mean "=="?