多个键多个值

时间:2016-03-21 00:00:18

标签: python excel dictionary

我正在尝试使用3个键detailsUsernamePassword创建一个字典Emailid,每个键包含多个值。我正在尝试从Excel工作簿中添加键的值。

我想访问字典的每个特定值

details{"Username":Value1,"Password":Value1,"Emailid":Value1}

等等。

**现在更新了代码

我试图从excel本身读取和存储密钥及其相应数据的值。这是更新代码

    import openpyxl
    wb = openpyxl.load_workbook('user_details.xlsx')
    WSheet = get_sheet_by_name('Sheet1')
    # get the active working sheet 1
       print('Reading rows...')
       # read header values into the list    
       keys = [sheet.cell(0, col_index).value for col_index in xrange(sheet.ncols)]
print "keys are", keys
dict_list = []
for row_index in xrange(1, sheet.nrows):
    assetData= {keys[col_index]: sheet.cell(row_index, col_index).value 
         for col_index in xrange(sheet.ncols)}
    dict_list.append(d)
print dict_list

Trying to print the value
for k,v in assetData.items():
    Print(%s "hi  %s  ,Records show that you have not paid dues " %  (+ str(assetData[assettagtype["k"]]) +str(assetData[assettagname["k"]]))
    # this line of code is just an idea ,syntactically incorrect

示例数据:

username    password    emailid
A123    alpha   abc@xyz.com
b123    tango   pqr@xyz.com
c123    charlie muv@xyz.com

1 个答案:

答案 0 :(得分:0)

我不确定这是不是你想要的,但按照你在这里说的那样:

  

我想访问字典的每个特定值

我猜你要迭代字典并获得每个值。为此,您可以使用dict.items()语法:

for key, value in details.items():
    print('{0}: {1}'.format(key, value))

如果您只需要这些值,则可以使用dict.values()

for value in details.values():
    print(value)