gpt4 book ai didi

python - 使用 Panda df.to_excel(...) 后获取 excel 文件

转载 作者:行者123 更新时间:2023-11-30 22:40:03 25 4
gpt4 key购买 nike

我正在使用 Pyrebase 将文件上传到 Firebase。

我有一个 DataFrame df 并将其转换为 Excel 文件,如下所示:

writer      = ExcelWriter('results.xlsx')
excelFile = df.to_excel(writer,'Sheet1')

print(excelFile)

# Save to firebase
childRef = "path/to/results.xlsx"

storage = firebase.storage()
storage.child(childRef).put(excelFile)

但是,这会将 Excel 文件存储为零字节的 Office 电子表格。如果我运行 writer.save() ,那么我确实会获得适当的文件类型 (xlsx ),但它存储在我的服务器上(我想避免这种情况)。如何生成正确的文件类型,就像使用 writer.save() 那样?

注意:print(excelFile) 返回None

最佳答案

可以通过使用本地内存来解决:

# init writer
bio = BytesIO()
writer = pd.ExcelWriter(bio, engine='xlsxwriter')
filename = "output.xlsx"

# sheets
dfValue.to_excel(writer, "sheetname")

# save the workbook
writer.save()
bio.seek(0)

# get the excel file (answers my question)
workbook = bio.read()
excelFile = workbook

# save the excelfile to firebase
# see also issue: https://github.com/thisbejim/Pyrebase/issues/142
timestamp = str(int(time.time()*1000));
childRef = "/path/to/" + filename

storage = firebase.storage()
storage.child(childRef).put(excelFile)
fileUrl = storage.child(childRef).get_url(None)

关于python - 使用 Panda df.to_excel(...) 后获取 excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42954054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com