读取多个csv写入不同excel工作表
时间: 2023-07-11 08:04:42 浏览: 154
VBA批量处理csv或其他excel文件数据
可以使用Python中的pandas库来实现。具体步骤如下:
1. 导入pandas库:
```python
import pandas as pd
```
2. 读取多个csv文件,将它们存储在一个列表中:
```python
file_list = ['file1.csv', 'file2.csv', 'file3.csv']
dfs = [pd.read_csv(file) for file in file_list]
```
3. 创建一个Excel文件对象:
```python
writer = pd.ExcelWriter('output.xlsx')
```
4. 将每个DataFrame写入Excel文件中的不同工作表:
```python
sheet_names = ['Sheet1', 'Sheet2', 'Sheet3']
for i, df in enumerate(dfs):
df.to_excel(writer, sheet_name=sheet_names[i], index=False)
```
5. 保存Excel文件:
```python
writer.save()
```
完整代码示例:
```python
import pandas as pd
file_list = ['file1.csv', 'file2.csv', 'file3.csv']
dfs = [pd.read_csv(file) for file in file_list]
writer = pd.ExcelWriter('output.xlsx')
sheet_names = ['Sheet1', 'Sheet2', 'Sheet3']
for i, df in enumerate(dfs):
df.to_excel(writer, sheet_name=sheet_names[i], index=False)
writer.save()
```
阅读全文