将CSV文件合并到一个与python的不同表格中的一个Excel文件中

发布于 2025-02-12 18:39:12 字数 140 浏览 4 评论 0原文

我有多个CSV文件,我想将它们移至带有不同表格的一个Excel文件。因此,我希望将每个CSV文件添加到Excel。如果有3个CSV文件,则应该是一个带有 3张纸的Excel文件,并且表格名称必须是CSV文件名。

最好的解决方案是什么?

I have multiple CSV files and I want to move them to one excel file with different sheets. So, I am looking to add each CSV file to a sheet of excel. If there are 3 CSV files, it should be one Excel file with 3 sheets, and sheet names must be the CSV file name.

What would be the best solution for this?

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

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

发布评论

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

评论(1

风筝有风,海豚有海 2025-02-19 18:39:12

尝试:

import pandas as pd
import pathlib

with pd.ExcelWriter('output.xlsx') as writer:
    for filename in pathlib.Path('.').glob('*.csv'):
        df = pd.read_csv(filename)
        df.to_excel(writer, sheet_name=filename.stem, index=False)

Try:

import pandas as pd
import pathlib

with pd.ExcelWriter('output.xlsx') as writer:
    for filename in pathlib.Path('.').glob('*.csv'):
        df = pd.read_csv(filename)
        df.to_excel(writer, sheet_name=filename.stem, index=False)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文