场景:多行excel文件,a列名为index,b列名为title,e列名为appkey,g列名为phone。excel文件中有数百行非空数据
需求:将以上值写入字典,格式要求为:
{'appkey': '8628c079ff484d2388a7649fc5bccfca', 'title': '售前咨询', 'index': '2021-09-02-155336', 'phone': '12345678912'}
{'appkey': '8628c079ff484d2388a7649fc5bccfca', 'title': '售前咨询', 'index': '2021-09-02-155336', 'phone': '12345678912'}
...
{'appkey': '8628c079ff484d2388a7649fc5bccfca', 'title': '售前咨询', 'index': '2021-09-02-155336', 'phone': '12345678912'}
问题:现在代码只能输出excel表中最后一行数据,不能满足需求。要求解答最后能返回上述换行、完整的数据
代码片段:
```python
from openpyxl import *
wb = load_workbook('test_1.xlsx')
ws = wb.active
'''
字典套字典
'''
data ={}
for row in range(2,ws.max_row+1):
#取单元格,值为字符串,格式为列名+行名
appkey = ws['E' + str(row)].value
title = ws['B' + str(row)].value
index = ws['A' + str(row)].value
phone = ws['G' + str(row)].value
data.update({'appkey':appkey,'title':title,'index':index,'phone':phone})
print(data)
```