把数据写入excel
时间: 2023-07-26 10:03:47 浏览: 75
好的,我可以为您提供一些写入Excel文件的方法,具体如下:
1. 使用Python内置模块`openpyxl`,示例代码如下:
```python
import openpyxl
# 创建一个工作簿
wb = openpyxl.Workbook()
# 创建一个工作表
ws = wb.active
# 写入数据
ws['A1'] = '姓名'
ws['B1'] = '年龄'
ws['C1'] = '性别'
ws['A2'] = '张三'
ws['B2'] = 20
ws['C2'] = '男'
# 保存工作簿
wb.save('example.xlsx')
```
2. 使用第三方库`pandas`,示例代码如下:
```python
import pandas as pd
# 创建一个数据框
df = pd.DataFrame({'姓名': ['张三', '李四', '王五'], '年龄': [20, 25, 30], '性别': ['男', '女', '男']})
# 写入Excel文件
df.to_excel('example.xlsx', index=False)
```
以上两种方法都可以将数据写入Excel文件中,您可以根据自己的需求选择使用哪种方法。
阅读全文
相关推荐












