将数据插入xlsx

发布于 2025-01-16 07:46:44 字数 502 浏览 3 评论 0原文

我正在运行一个 python 脚本,它的结果是一组值。假设结果是唯一的日期和时间。

date = now.strftime("%d/%m/%Y")
time = now.strftime("%H:%M:%S")

我想将数据写入 xlsx 文件,但问题是每次脚本完成时数据都会被重写。

我应该如何编写日期以将其添加到 xlsx 中而不是重写它的第一行? 这是我正在使用的代码,我不知道如何更改它。

worksheet.write(1, 0, date)
worksheet.write(1, 1, time)

我希望最终得到的结果应该如下所示:

Date         Time
20/03/2022   00:24:36
20/03/2022   00:55:36
21/03/2022   15:24:36
22/03/2022   11:24:36
23/03/2022   22:24:36

I'm running a python script and the result of it is a group of values. Let's say the result is a unique date and time.

date = now.strftime("%d/%m/%Y")
time = now.strftime("%H:%M:%S")

I would like to write down the data into xlsx file, but the problem is that the data is rewrite every single time the scrip is done.

How should I write the date to add it into the xlsx and not to rewrite the first line of it?
That's the code I am using and I'm not sure how to change it.

worksheet.write(1, 0, date)
worksheet.write(1, 1, time)

The result I would like to get at the end should be something like following:

Date         Time
20/03/2022   00:24:36
20/03/2022   00:55:36
21/03/2022   15:24:36
22/03/2022   11:24:36
23/03/2022   22:24:36

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲影 2025-01-23 07:46:44

您可以以追加模式打开 Excel,然后继续插入数据。请参考下面的片段:

with pd.ExcelWriter("existing_file_name.xlsx", engine="openpyxl", mode="a") as writer:
    df.to_excel(writer, sheet_name="name")

You can open the excel in append mode and then keep inserting data. Refer below snippet:

with pd.ExcelWriter("existing_file_name.xlsx", engine="openpyxl", mode="a") as writer:
    df.to_excel(writer, sheet_name="name")
我们只是彼此的过ke 2025-01-23 07:46:44

最简单的方法是将数据转换为 Pandas 数据框,然后您可以使用以下简单命令轻松将 dataframe 转换为 xlxs 文件:

df.to_excel("filename.xlxs")

The simplest way is to convert your data into a Pandas dataframe, then you can easily convert your dataframe into xlxs file by using a simple command like:

df.to_excel("filename.xlxs")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文