python 数据存储为excel
时间: 2023-08-26 15:14:59 浏览: 140
要将Python数据存储为Excel,可以使用Python的openpyxl库来实现。以下是三个不同的示例,介绍了如何使用openpyxl库将数据写入Excel文件:
1. 使用字典将数据写入Excel文件:
```python
import openpyxl
dict_data = {
"key1": ["test1", "test2", "test3"],
"key2": ["test4", "test5", "test6"],
"key3": ["test7", "test8", "test9"]
}
keys = list(dict_data.keys())
values = list(dict_data.values())
wb = openpyxl.Workbook()
ws = wb.create_sheet('sheet1')
for key in keys:
column = keys.index(key)
row_value = values[column]
ws.cell(row=1, column=column+1).value = key
for value in row_value:
row = row_value.index(value) + 2
ws.cell(row=row, column=column+1).value = value
wb.save('保存的数据1.xlsx')
```
2. 使用列表将数据写入Excel文件:
```python
import openpyxl
movie_names = ['a', 'b', 'c']
directors = ['bob', 'james', 'laker']
scores = [9.6, 9.0, 8.0]
comments = ['good', 'nice', 'bad']
try:
workbook = openpyxl.load_workbook('test.xlsx')
except Exception as e:
workbook = openpyxl.Workbook()
sheet_name = workbook.active
my_title = ['电影名称', '导演', '评分', '评论']
sheet_name.append(my_title)
for i in range(4):
if i == 0:
for j, value in enumerate(movie_names):
sheet_name.cell(row=j+2, column=i+1, value=value)
if i == 1:
for m, value in enumerate(directors):
sheet_name.cell(row=m+2, column=i+1, value=value)
if i == 2:
for n, value in enumerate(scores):
sheet_name.cell(row=n+2, column=i+1, value=value)
if i == 3:
for o, value in enumerate(comments):
sheet_name.cell(row=o+2, column=i+1, value=value)
workbook.save('test.xlsx')
workbook.close()
```
3. 使用循环将数据写入Excel文件:
```python
import openpyxl
datalist = [['www', 'www图片', '西游记', 'xiyouji', '100分', '0人', '很好', '超级棒'],
['www2', 'www图片2', '西游记2', 'xiyouji2', '1000分', '1人', '很棒', '一级棒']]
wb = openpyxl.Workbook()
sheet = wb.active
for i in range(2):
data = datalist[i]
for j in range(8):
sheet.cell(row=i+1, column=j+1, value=data[j])
wb.save('test.xlsx')
```
以上是三个不同的示例,它们分别展示了使用字典、列表和循环将数据写入Excel文件的方法。根据你的需求选择其中一个示例,并将数据替换成你想要写入Excel文件的数据即可。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [Python字段数据保存到excel表格](https://blog.csdn.net/sl01224318/article/details/123025023)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [Python将数据写入excel](https://blog.csdn.net/qq_40216188/article/details/128410706)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [python实现将数据写入Excel文件中](https://blog.csdn.net/guoxuying/article/details/112470819)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文