简体   繁体   English

如何调用和遍历字典的特定索引?

[英]How to call and iterate through specific indexes of a dictionary?

I currently have code that prints out a username and their password by means of dictionary. 我目前有通过字典打印出用户名和密码的代码。 The output is username:abc password:12345. 输出为username:abc password:12345。 Right now I have them all just printing out at the same time. 现在,我把它们都同时打印出来。 My main goal is to be able to email these users based on their usernames, but of course I only want to include their username and password information. 我的主要目标是能够根据用户名发送电子邮件给这些用户,但是我当然只希望包括他们的用户名和密码信息。 Not the whole list of information. 不是全部信息列表。 So basically I want to be able to tell the specific user only their password. 因此,基本上,我希望仅告诉特定用户其密码。 For example, I would want to send an email to user abc and that email should only contain their username and password. 例如,我想向用户abc发送电子邮件,并且该电子邮件应仅包含其用户名和密码。 Then I would like to send an email to user xyz but that should only contain the password of user xyz. 然后,我想向用户xyz发送电子邮件,但该电子邮件只应包含用户xyz的密码。 Is there a way I can make this happen? 有什么办法可以做到这一点?

I currently have everything from the dictionary printing. 我目前拥有字典印刷中的所有内容。 So all the usernames and passwords are being printed out. 因此,所有用户名和密码都已打印出来。 How can I iterate through these and send an email to each user with their password? 我该如何遍历这些密码并向每个用户发送带有密码的电子邮件?

    lines = []
    User_Pass = {}
    #Open output file and show only lines that contain 'Success'
    with open("output.txt", "r") as f:
        for line in f.readlines():
            #Get rid of everything in line after 'Success'
            if 'Success' in line:
                lines.append(line[:line.find("Success")-1])

    for element in lines:
        parts = element.strip().split(":")
        User_Pass.update({parts[0] : parts[1]})

    for key, value in User_pass.items():
        print('Username: ' + key + ' Password: ' + value)

I want to be able to email each username and tell them their password. 我希望能够通过电子邮件发送每个用户名并告诉他们其密码。 I am really not sure how to go about this. 我真的不确定如何去做。 Any help would be appreciated! 任何帮助,将不胜感激!

Assuming you have the dictionary constructed, you just ask it for the value associated with the key , which is the username: 假设您已经构造了字典,您只需要向它询问与key相关联的 ,即用户名:

user_pw = User_Pass.get(username)

this will return None if the username is not in the dictionary. 如果用户名不在词典中,则将返回None。

Suggest you research Python dictionaries for some examples. 建议您研究一些示例的Python词典。 In your loop code, you have it a little backwards as well. 在循环代码中,您也有一些倒退。 Anytime you want to iterate through a dictionary (the right way) you want to do it with the keys , not the items, so to loop through all in your example: 每当您想遍历字典(正确的方式)时,都想使用而不是项来遍历,因此可以遍历示例中的所有内容:

for key in User_Pass.keys():
    print (key, User_Pass.get(key) )

Only fill your User_Pass dictionary when you meet "username" or "password": 仅在遇到“用户名”或“密码”时填写User_Pass词典:

for element in lines:
    key, value = element.strip().split(":")
    if key in {"username", "password"}:
        User_Pass.update({key: value})

A simple demonstration: 一个简单的演示:

lines = [
    "fullname:John Doe",
    "username:jdoe",
    "password:S3cRet",
    "age:45",
]

User_Pass = {}
for element in lines:
    key, value = element.strip().split(":")
    if key in {"username", "password"}:
        User_Pass.update({key: value})

print(User_Pass)

You get: 你得到:

{'username': 'jdoe', 'password': 'S3cRet'}

Please, you should follow the naming conventions of the PEP8 : "User_Pass" => "user_pass". 请,您应该遵循PEP8的命名约定:“ User_Pass” =>“ user_pass”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM