简体   繁体   English

df.to_csv 适用于 S3 存储桶,但 df.to_excel 不适用

[英]Df.to_csv works with S3 bucket, but df.to_excel does not

I will try to phrase this the best I can.我会尽力表达这一点。 I want to write/append to an xls/xlsx file from Sagemaker that is in an S3 bucket.我想从 Sagemaker 写入/附加到 S3 存储桶中的 xls/xlsx 文件。 There is an empty excel sheet for each file type (csv,xls,xlsx) in the S3 bucket that I upload prior to.我之前上传的 S3 存储桶中的每种文件类型(csv、xls、xlsx)都有一个空的 Excel 表。 I am able to write/append a df to the empty csv file in the S3 bucket no problem.我能够将 df 写入/附加到 S3 存储桶中的空 csv 文件没有问题。 But it does not work for xls/xlsx.但它不适用于 xls/xlsx。 Here is the code I am using for the csv:这是我用于 csv 的代码:

df.to_csv('s3://bucket_name/temp/Database.csv', index=False, mode = 'w', header = False)

Here is the code I am using for the xlsx file:这是我用于 xlsx 文件的代码:

with pd.ExcelWriter('s3://bucket_name/project/Database.xlsx', mode = 'w', engine="xlsxwriter") as writer:
    df.to_excel(writer, "Sheet 1")  
    writer.save()

Note: For xls, I just change the engine to openpyxl and change the file path to the xls one注意:对于xls,我只是将引擎更改为openpyxl并将文件路径更改为xls之一

I get this from the above code when running for xlsx/xls:在为 xlsx/xls 运行时,我从上面的代码中得到了这个:

FileCreateError: [Errno 2] No such file or directory: 's3://bucket_name/project/Database.xlsx'

Even though it is in the exact same location as the other one.即使它与另一个位于完全相同的位置。 I am not sure what the problem is but I have not found any solution.我不确定问题是什么,但我还没有找到任何解决方案。 I have tried adding 'r' to make it a raw string, I have tried changing the slashes around, but nothing seems to work.我尝试添加 'r' 以使其成为原始字符串,我尝试更改斜杠,但似乎没有任何效果。 Does anyone that has experience with xlsxwriter/openpyxl know what the problem could be?有没有使用 xlsxwriter/openpyxl 经验的人知道问题可能是什么?

pd.to_csv can handle s3 paths since v0.20.0 , but pd.ExcelWriter can't. pd.to_csv可以处理自v0.20.0以来的 s3 路径,但pd.ExcelWriter不能。

You'll need to s3fs or boto like explained here: Store Excel file exported from Pandas in AWS .您需要像这里解释的那样使用s3fs或 boto: 将从 Pandas 导出的 Excel 文件存储在 AWS 中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM