多个CSV合并为1个EXCEL的不同sheet
时间: 2023-03-30 07:00:20 浏览: 111
可以使用Python中的pandas库来实现多个CSV合并为一个Excel的不同sheet。具体步骤如下:
1.导入pandas库
import pandas as pd
2.读取CSV文件并合并
df1 = pd.read_csv('file1.csv')
df2 = pd.read_csv('file2.csv')
df3 = pd.read_csv('file3.csv')
df = pd.concat([df1, df2, df3])
3.将合并后的数据写入Excel文件
writer = pd.ExcelWriter('output.xlsx')
df.to_excel(writer, sheet_name='Sheet1')
writer.save()
这样就可以将多个CSV文件合并为一个Excel文件的不同sheet了。
相关问题
python合并多个csv为一个excel的多个sheet
可以使用 pandas 库来合并多个 CSV 文件,并将它们保存为一个 Excel 文件的多个 sheet。以下是一个示例代码:
```python
import pandas as pd
# 假设有三个 CSV 文件:file1.csv、file2.csv 和 file3.csv
file_names = ['file1.csv', 'file2.csv', 'file3.csv']
# 读取每个 CSV 文件,并将它们存储在一个列表中
dfs = [pd.read_csv(filename) for filename in file_names]
# 创建一个 Excel 写入器
writer = pd.ExcelWriter('output.xlsx')
# 将每个 DataFrame 存储为 Excel 的一个 sheet
for i, df in enumerate(dfs):
df.to_excel(writer, sheet_name=f'Sheet{i+1}', index=False)
# 保存 Excel 文件
writer.save()
```
这个代码会将三个 CSV 文件合并到一个名为 `output.xlsx` 的 Excel 文件中,并将它们分别存储在名为 `Sheet1`、`Sheet2` 和 `Sheet3` 的 sheet 中。你可以根据需要修改代码,以适应你的具体情况。
python合并多个csv文件为一个excel表不同工作簿
您可以使用Python中的pandas库来合并多个CSV文件为一个Excel表不同工作簿。以下是一个示例代码,假设您已经安装了pandas库:
```python
import pandas as pd
# 定义要合并的CSV文件列表
csv_files = ["file1.csv", "file2.csv", "file3.csv"]
# 创建一个空的Excel文件
writer = pd.ExcelWriter("output.xlsx", engine="xlsxwriter")
# 遍历CSV文件列表,逐个读取并写入Excel文件中
for csv_file in csv_files:
# 读取CSV文件
df = pd.read_csv(csv_file)
# 获取文件名作为工作表名
sheet_name = csv_file.split(".")[0]
# 将数据写入Excel文件中的新工作表
df.to_excel(writer, sheet_name=sheet_name, index=False)
# 保存Excel文件
writer.save()
```
这段代码将多个CSV文件合并为一个Excel文件,并将每个CSV文件写入Excel文件中的新工作表中,工作表名与CSV文件名相同。您可以根据需要进行修改。
阅读全文
相关推荐















