Python从excel数据创建字典 [英] Python Creating Dictionary from excel data

查看:112
本文介绍了Python从excel数据创建字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从值创建一个字典,我从excel单元格中获取,我的代码如下,

I want to create a dictionary from the values, i get from excel cells, My code is below,

wb = xlrd.open_workbook('foo.xls')
sh = wb.sheet_by_index(2)   
for i in range(138):
    cell_value_class = sh.cell(i,2).value
    cell_value_id = sh.cell(i,0).value

我想创建一个字典,如下所示,其中包含来自 excel 单元格的值;

and I want to create a dictionary, like below, that consists of the values coming from the excel cells;

{'class1': 1, 'class2': 3, 'class3': 4, 'classN':N}

知道如何创建这本词典吗?

Any idea on how I can create this dictionary?

推荐答案

d = {}
wb = xlrd.open_workbook('foo.xls')
sh = wb.sheet_by_index(2)   
for i in range(138):
    cell_value_class = sh.cell(i,2).value
    cell_value_id = sh.cell(i,0).value
    d[cell_value_class] = cell_value_id

这篇关于Python从excel数据创建字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆