解码UTF-8编码的CSV文件时出错

发布于 2025-01-17 17:22:50 字数 1811 浏览 1 评论 0原文

第一次在这里发帖:)

我在尝试从电子邮件收件箱下载所有附件时遇到问题。 我下载它们,然后将它们写入一个文件,并指定其路径。 它非常适合直接下载到文件的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温馨耳语 2025-01-24 17:22:50

https://dmorgan.info/posts/encoded-word-syntax/”中被盗。

import re
import base64
import quopri

def encoded_words_to_text(encoded_words):
    encoded_word_regex = r'=\?{1}(.+)\?{1}([B|Q])\?{1}(.+)\?{1}='
    charset, encoding, encoded_text = re.match(encoded_word_regex,
                                               encoded_words).groups()
    if encoding == 'B':
        byte_string = base64.b64decode(encoded_text)
    elif encoding == 'Q':
        byte_string = quopri.decodestring(encoded_text)
    return byte_string.decode(charset)

filename = '=?UTF-8?B?RXh0cmFjdCBzZXggZ8OpIHB1YmxpYy0yMDIyLTAzLTI2LTAwLTAwLTI2LmNzdg==?='
encoded_words_to_text(filename)
 '提取性爱公共-2022-03-26-00-00-26.csv'
 

1 和消除语法:“ IS”具有字面意思。您的意思是“ ==”?

Stolen from Encoded-word Syntax1:

import re
import base64
import quopri

def encoded_words_to_text(encoded_words):
    encoded_word_regex = r'=\?{1}(.+)\?{1}([B|Q])\?{1}(.+)\?{1}='
    charset, encoding, encoded_text = re.match(encoded_word_regex,
                                               encoded_words).groups()
    if encoding == 'B':
        byte_string = base64.b64decode(encoded_text)
    elif encoding == 'Q':
        byte_string = quopri.decodestring(encoded_text)
    return byte_string.decode(charset)

Apply as follows:

filename = '=?UTF-8?B?RXh0cmFjdCBzZXggZ8OpIHB1YmxpYy0yMDIyLTAzLTI2LTAwLTAwLTI2LmNzdg==?='
encoded_words_to_text(filename)
'Extract sex gé public-2022-03-26-00-00-26.csv'

1 and eliminated SyntaxWarning: "is" with a literal. Did you mean "=="?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文