937135952 发表于 2021-9-7 15:50:55

怎么把带字典的列表写入excel

本帖最后由 937135952 于 2021-9-7 15:52 编辑

res = [{"city":"重庆","people":"10万"},{"city":"北京","people":"20万"},{"city":"上海","people":"30万"}]



    wb = openpyxl.Workbook()
    ws = wb.active
    ws['A1']="城市"
    ws['B1']="人口"
   
    for each in results:
      ws.append(each)      
    wb.save("城市人口情况.xlsx")   

比如上述情况,怎么才能写入excel呢,我知道如果each是一个列表的话,按这种方法就可以直接写入,但是是字典的话不知道要怎么做,还有[{"city":"重庆","people":"10万"},{"city":"北京","people":"20万"},{"city":"上海","people":"30万"}]这个东西应该怎么叫,字典列表?

suchocolate 发表于 2021-9-7 16:07:03

wb = openpyxl.Workbook()
ws = wb.active
ws['A1'] = "城市"
ws['B1'] = "人口"

res = [{"city": "重庆", "people": "10万"}, {"city": "北京", "people": "20万"}, {"city": "上海", "people": "30万"}]
for each in res:
    w = , each['people']]
    ws.append(w)
wb.save("城市人口情况.xlsx")
页: [1]
查看完整版本: 怎么把带字典的列表写入excel