首页 > 解决方案 > 无法从熊猫将 xlsx 写入 GCS

问题描述

我有一个奇怪的问题。

我从作为数据管道的气流触发 K8S 作业。最后,我需要将数据帧作为.parquet.xlsx文件写入谷歌云存储。

[...]
export_app.to_parquet(f"{output_path}.parquet")
export_app.to_excel(f"{output_path}.xlsx")

镶木地板文件一切正常,但 xlsx 出现错误。

严重性:“INFO”
textPayload:“[Errno 2] 没有这样的文件或目录:'gs://my_bucket/incidents/prediction/2020-04-29_incidents_result.xlsx'

我尝试将文件写为 csv 来尝试

export_app.to_parquet(f"{output_path}.parquet")
export_app.to_csv(f"{output_path}.csv")
export_app.to_excel(f"{output_path}.xlsx")

我每次都收到相同的消息,并且按预期找到了另一个文件。

写 xlsx 文件有什么限制吗?

openpyxl在我的环境中安装了这个包。

标签: pythonexcelpandasgoogle-cloud-storage

解决方案


根据要求,我传递了一些代码,我如何直接使用 gcs python3 api 创建新的 xlsx 文件。我使用了这个教程和这个api 参考

# Imports the Google Cloud client library
from google.cloud import storage

# Instantiates a client
storage_client = storage.Client()

# Create the bucket object
bucket = storage_client.get_bucket("my-new-bucket")

#Confirm bucket connected
print("Bucket {} connected.".format(bucket.name))

#Create file in the bucket
blob = bucket.blob('test.xlsx')
with open("/home/vitooh/test.xlsx", "rb") as my_file:
    blob.upload_from_file(my_file)

我希望它会有所帮助!


推荐阅读