Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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文件作为附件发送,同时处理前导零_Python_Csv - Fatal编程技术网

如何使用python将csv文件作为附件发送,同时处理前导零

如何使用python将csv文件作为附件发送,同时处理前导零,python,csv,Python,Csv,我想使用CSV文件作为附件发送电子邮件,当我打开电子邮件时,我收到一个附件,其中没有前导零,我希望前导零出现在使用python的CSV文件附件中,请建议我解决方法 结果文件是CSV文件: 示例数据如下所示:0763808075 但是当我运行python脚本时,电子邮件附件的数据如下所示:763808075 我希望在电子邮件附件中添加前导零 with open ( resultsfile1 , "rb" ) as attached_file : part = M

我想使用CSV文件作为附件发送电子邮件,当我打开电子邮件时,我收到一个附件,其中没有前导零,我希望前导零出现在使用python的CSV文件附件中,请建议我解决方法

结果文件是CSV文件: 示例数据如下所示:0763808075

但是当我运行python脚本时,电子邮件附件的数据如下所示:763808075

我希望在电子邮件附件中添加前导零

with open ( resultsfile1 , "rb" ) as attached_file :
        part = MIMEApplication (
            attached_file.read() ,
            Name=resultsfile1
        )
    # After the file is closed
result= os.path.basename ('\filename.csv')
part['Content-Disposition'] = 'attachment; filename={} '.format(str(result))
msg.attach ( part )

如果在文本编辑器(不是电子表格)中打开附件,是否存在前导零?是的,它存在,但在csv或excel中不存在,因此,我需要一个解决方案,它将帮助我确保在用户打开通过Gmail发送的文件时,附件中存在前导零。不使用Excel几乎总是正确的解决方案。你的Python代码很好。但是为什么要以二进制文件打开文件?CSV本质上是一种文本格式。有没有办法在附件中附加CSV文件,并保留前导零,我们需要在报告中附加电话号码,电话号码从0开始,发送此信息非常重要,即使将打开的文件更改为文本,前导零问题仍然存在