要将Python列表存储到Excel表格中,您可以使用 `openpyxl` 或 `pandas` 库。以下是使用这两个库的示例代码:
使用 `openpyxl` 库
```python
import openpyxl
wb = openpyxl.Workbook()
选择要添加数据的工作表
ws = wb.active
ws.title = '明细'
准备要写入的数据
data = [
['A', 'B', 'C', 'D', 'E'],
[1, 2, 4, 6, 8],
[4, 6, 7, 9, 0],
[2, 6, 4, 5, 8]
]
逐行逐列写入数据
for r in range(len(data)):
for c in range(len(data)):
ws.cell(row=r + 1, column=c + 1, value=data[r][c])
保存工作簿到文件
wb.save('data.xlsx')
print('成功写入文件: data.xlsx')
使用 `pandas` 库
```python
import pandas as pd
准备要写入的数据
data = {
'A': [1, 2, 4, 6, 8],
'B': [4, 6, 7, 9, 0],
'C': [2, 6, 4, 5, 8]
}
创建一个DataFrame
df = pd.DataFrame(data)
将数据存储到Excel文件
df.to_excel('data.xlsx', index=False)
print('成功写入文件: data.xlsx')
请根据您的需求选择合适的库和代码示例。