Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在电子邮件中将熊猫数据框作为.csv附加,而不使用.to_csv_Python_Pandas_Amazon Web Services_Aws Lambda - Fatal编程技术网

Python 在电子邮件中将熊猫数据框作为.csv附加,而不使用.to_csv

Python 在电子邮件中将熊猫数据框作为.csv附加,而不使用.to_csv,python,pandas,amazon-web-services,aws-lambda,Python,Pandas,Amazon Web Services,Aws Lambda,我有一个熊猫数据框,我想在电子邮件附件中以.csv文件的形式发送出去。现在,当我使用df.to_csv()时,每次都会下载文件 我不想将文件保存在系统中,只想直接将其作为.csv文件传递。有办法做到这一点吗 import pandas as pd df = pd.DataFrame(output of sqlquery) # this data is dataframe output of a sql query. def send_email(sender, recipient, aws

我有一个熊猫数据框,我想在电子邮件附件中以.csv文件的形式发送出去。现在,当我使用df.to_csv()时,每次都会下载文件

我不想将文件保存在系统中,只想直接将其作为.csv文件传递。有办法做到这一点吗

import pandas as pd

df = pd.DataFrame(output of sqlquery) # this data is dataframe output of a sql query. 

def send_email(sender, recipient, aws_region, subject, df):

client = boto3.client('ses', region_name=aws_region)

BODY_TEXT = "Hello,\r\nPlease find the attached file."
BODY_HTML = """\
<html>
<head></head>
<body>
<h1>Hello!</h1>
<p>Please find the attached file.</p>
</body>
</html>
"""

msg = MIMEMultipart('mixed')
msg['From'] = sender
print(msg['From'])
msg['To'] = recipient
msg['Subject'] = 'TOI Order Alert'

# The character encoding for the email.
CHARSET = "UTF-8"

msg_body = MIMEMultipart('alternative')
textpart = MIMEText(BODY_TEXT.encode(CHARSET), 'plain', CHARSET)
htmlpart = MIMEText(BODY_HTML.encode(CHARSET), 'html', CHARSET)


# Add the text and HTML parts to the child container.
msg_body.attach(textpart)
msg_body.attach(htmlpart)





# # Define the attachment part and encode it using MIMEApplication.
att = MIMEApplication(df.to_csv('test.csv'))
att.add_header('Content-Disposition','attachment; filename='+ 'test.csv')

# Attach the multipart/alternative child container to the multipart/mixed
# parent container.
msg.attach(msg_body)

# Add the attachment to the parent container.
msg.attach(att)



#Provide the contents of the email.
response = client.send_raw_email(
        Source=msg['From'],
        Destinations=[
            msg['To']
        ],
        RawMessage={
            'Data':msg.as_string(),
        }
    )
 
将熊猫作为pd导入
df=pd.DataFrame(sqlquery的输出)#此数据是sql查询的数据帧输出。
def发送电子邮件(发件人、收件人、aws_地区、主题、df):
客户机=boto3.客户机('ses',地区名称=aws\U地区)
BODY\u TEXT=“您好,\r\n请查找附件。”
BODY_HTML=“”\
你好
请查找附件

""" msg=MIMEMultipart('mixed') msg['From']=发件人 打印(消息['From']) msg['To']=收件人 msg['Subject']=“TOI订单警报” #电子邮件的字符编码。 CHARSET=“UTF-8” msg_body=MIMEMultipart('备选方案') textpart=MIMEText(BODY_TEXT.encode(字符集),‘普通’,字符集) htmlpart=MIMEText(BODY_HTML.encode(CHARSET),'HTML',CHARSET) #将文本和HTML部分添加到子容器中。 msg_body.attach(文本部分) msg_body.attach(htmlpart) ##定义附件部件并使用MIMEApplication对其进行编码。 att=MIMEApplication(df.to_csv('test.csv')) att.add_标题('Content-Disposition','attachment;filename='+'test.csv')) #将multipart/alternative子容器附加到multipart/mixed子容器 #父容器。 msg.attach(msg_body) #将附件添加到父容器。 附加信息(附件) #提供电子邮件的内容。 响应=client.send_raw_电子邮件( Source=msg['From'], 目的地=[ 味精['To'] ], 原始信息={ “数据”:msg.as_string(), } )
我建议如下

import io

s_buf = io.StringIO() 
df.to_csv(s_buf)
byte_buf = s_buf.encode()

并传入字节缓冲区

为什么?这仍然迫使我将df保存到本地数据库中。我正在尝试不这样做。是否要避免在本地文件系统上生成csv?如果是这种情况,StringIO将创建一个包含csv文件的内存缓冲区。然后你可以将它发送到任何八位字节流,明白了吗,所以我的新代码是:s_buf=io.BytesIO()df.to_csv(s_buf)##定义附件部分并使用MIMEApplication对其进行编码。att=MIMEApplication(EXPORTERS['dataframe.csv'](df))att=MIMEApplication(s_buf)不幸的是,我仍然收到一条失败的消息:“errorMessage”:“需要一个类似字节的对象,而不是'str'”,我已经更新了答案并将字符串转换为类似字节的对象。新错误:“errorMessage”:“通常情况下,编码时没有字符串参数,当我需要这样做时,我会将附件保存为tempfile,当它的分数完成时,它会被自动删除。您可以查看文档。评估一次,无论它是否符合您的要求@mohit我正在尝试通过ses发送文件,我已经突破了10mb的限制。关于如何发送或压缩该文件,你有什么想法吗?你可以压缩它并尝试发送,但最好将其存储在S3中,并在电子邮件中提供一个公共的或预签名的、有时间限制的URL。