将Excel中的列转换为python中的字典

将Excel中的列转换为python中的字典,python,excel,dictionary,xlrd,Python,Excel,Dictionary,Xlrd,我的excel文件中有三列,我必须使用xlrd将前两列的所有行转换为字典 预期产出: {'Sam' : 'Tester', 'John' : 'Developer', 'Fin' : 'Tester'} 代码打印前两列。如何转换为字典类型?首先,字典包含在{}not[]中,因此预期的输出应该是{'Sam':'Tester','John':'Developer','Fin':'Tester'} 此代码应适用于您: my_dict = {} for i in range(1, worksheet

我的excel文件中有三列,我必须使用xlrd将前两列的所有行转换为字典

预期产出:

{'Sam' : 'Tester', 'John' : 'Developer', 'Fin' : 'Tester'}


代码打印前两列。如何转换为字典类型?

首先,字典包含在{}not[]中,因此预期的输出应该是
{'Sam':'Tester','John':'Developer','Fin':'Tester'}

此代码应适用于您:

my_dict = {}
for i in range(1, worksheet.nrows):
    row = worksheet.row_values(i)
    my_dict[row[0]] = row[1]

print(my_dict)
my_dict = {}
for i in range(1, worksheet.nrows):
    row = worksheet.row_values(i)
    my_dict[row[0]] = row[1]

print(my_dict)