多个键多个值

2024-10-03 19:30:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试制作一个字典details,其中有3个键UsernamePasswordEmailid,每个键都包含多个值。我正在尝试为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

Tags: theinforindexvaluecolkeysdetails
1条回答
网友
1楼 · 发布于 2024-10-03 19:30:38

我不确定这是否是你要找的,但按照你在这里说的:

I want to access each particular value for the dictionary

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

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

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

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

相关问题 更多 >