循环登录凭据-Python字典
一般来说,Python和编程都是新手。目前,我正在尝试使用嵌套在字典中的字典来循环登录凭据。我的字典是这样的:循环登录凭据-Python字典,python,loops,for-loop,dictionary,Python,Loops,For Loop,Dictionary,一般来说,Python和编程都是新手。目前,我正在尝试使用嵌套在字典中的字典来循环登录凭据。我的字典是这样的: loginCredentials = {'Company1':{'User':'UserName', 'Password':'UserPassword', 'StoreID':'StoreNum'}, 'Company2':{'User':'UserName', 'Password':'UserPassword', 'StoreID':'St
loginCredentials = {'Company1':{'User':'UserName', 'Password':'UserPassword', 'StoreID':'StoreNum'},
'Company2':{'User':'UserName', 'Password':'UserPassword', 'StoreID':'StoreNum'}}
我正在尝试使用与公司1相关联的值登录,并在这些步骤完成后遵循一定数量的步骤,然后我想继续使用公司2等等
for循环是什么样子的?以及在输入每个步骤的登录条件时如何设置要调用的键
提前谢谢你
>>> loginCredentials = {'Company1':{'User':'UserName', 'Password':'UserPassword', 'StoreID':'StoreNum'}, 'Company2':{'User':'UserName', 'Password':'UserPassword', 'StoreID':'StoreNum'}}
>>> for company, credentials in loginCredentials.iteritems():
... store_id = credentials["StoreID"]
... password = credentials["Password"]
... username = credentials["User"]
... # do stuff
...
iteritems
将字典转换为元组列表(实际上是迭代器对象),其中包含字典项的键和值。因此,这里实际发生的是,您正在循环该列表,元组被解压到公司
和凭证
>>> list(loginCredentials.iteritems())
[('Company2', {'StoreID': 'StoreNum', 'Password': 'UserPassword', 'User': 'UserName'}), ('Company1', {'StoreID': 'StoreNum', 'Password': 'UserPassword', 'User': 'UserName'})]
python字典:
非常感谢你,我一直在阅读有关词典等的书籍,但我缺乏经验,这可能会让人有点困惑,所以我非常感谢你的解释。
for company, creds in loginCredentials.iteritems()
login(company, creds["User"], creds["Password"], creds["StoreID"])