简体   繁体   English

如何在每一行中拉出列表的特定部分?

[英]How to pull specific parts of a list on each line?

I have a list that spits out information like this: ['username', 'password'], ['username', 'password'], ['username', 'password'] , and so on..我有一个列表,它会吐出这样的信息: ['username', 'password'], ['username', 'password'], ['username', 'password']等等..

I would like to be able to pull a specific username and password later on.我希望以后能够提取特定的用户名和密码。 For example: ['abc', '9876'], ['xyz', '1234']例如: ['abc', '9876'], ['xyz', '1234']

pull abc and tell them the password is 9876 .abc并告诉他们密码是9876 Then pull xyz and tell them the password is 1234然后拉xyz告诉他们密码是1234

I tried messing around with the list and I am just drawing a blank on how to do this.我试着弄乱列表,我只是在如何做到这一点上画了一个空白。

    lines = []
    with open("output.txt", "r") as f:
        for line in f.readlines():
            if 'Success' in line:
                #get rid of everything after word success so only username and password is printed out
                lines.append(line[:line.find("Success")-1])
    for element in lines:
        #split username and password up at : so they are separate entities
        #original output was username:password, want it to be username, password
        parts = element.strip().split(":")
        print(parts)

I want to pull each username and then pull their password as described above我想提取每个用户名,然后如上所述提取他们的密码

Current output after running through this is ['username', 'password'] .通过此运行后的当前输出是['username', 'password'] The original output file had extra information that I got rid of which is what the code involving 'Success' took care of原始输出文件有我删除的额外信息,这些信息是涉及“成功”的代码处理的

I would like to do this without hardcoding a username in to it.我想在没有硬编码用户名的情况下做到这一点。 I am trying to automate this process so that it runs through every username and formats it to say, "hi [username}, your password is [123]" , for all of the usernames我正在尝试自动执行此过程,以便它遍历每个用户名并将其格式化为"hi [username}, your password is [123]" ,对于所有用户名

I then later would like to be able to only tell the specific user their password.后来我希望能够只告诉特定用户他们的密码。 For example, i want to send an email to user abc.例如,我想向用户 abc 发送一封电子邮件。 that email should only contain the username and password of user abc该电子邮件应仅包含用户 abc 的用户名和密码

Instead of printing parts , append them to a list.不要打印parts ,而是将它们附加到列表中。

data = []
for element in lines:
    parts = element.strip().split(":")
    data.append(parts)

Then you could convert these into a dictionary for lookup然后你可以将这些转换成字典进行查找

username_passwords = dict(data)
print(username_passwords['abc'])

If I am understanding this correctly parts is the list that contains [Username:Password].如果我理解正确,部分是包含 [用户名:密码] 的列表。 If that is the case we can assign each value of parts which should only have 2 elements in it to a dictionary as a dictionary pair and then call the username later on.如果是这种情况,我们可以将应该只有 2 个元素的部分的每个值分配给字典作为字典对,然后稍后调用用户名。

lines = []
User_Pass = {}
    with open("output.txt", "r") as f:
        for line in f.readlines():
            if 'Success' in line:
                #get rid of everything after word success so only username and password is printed out
                lines.append(line[:line.find("Success")-1])
    for element in lines:
        #split username and password up at : so they are separate entities
        parts = element.strip().split(":")
        User_Pass.update({parts[0] : parts[1]})

Then you can call the password from the username as follows if you know the username:然后,如果您知道用户名,则可以从用户名中调用密码,如下所示:

 x = User_Pass["foo"]

Or as you stated in the comments:或者如您在评论中所述:

for key, value in User_Pass.items():
    print('Username ' + key + ' Has a Password of ' + value)

it looks like after you do this看起来像你这样做之后

lines.append(line[:line.find("Success")-1])

lines = ['username:password', 'username:password'...]行 = ['用户名:密码','用户名:密码'...]

so I would do this所以我会这样做

new_list_of_lists = [element.strip().split(":") for element in lines]

new_list_of_lists should now look like [[username, password], [username, password]] new_list_of_lists 现在应该看起来像 [[username, password], [username, password]]

then just do this:然后就这样做:

dict_of_usernames_and_passwords = dict(new_list_of_lists)

with a dict you can have now retrieve passwords using usernames.使用字典,您现在可以使用用户名检索密码。 like:喜欢:

dict_of_usernames_and_passwords['abc']

you can save the dict, using json module, to a file, for easy retrieval.您可以使用 json 模块将 dict 保存到文件中,以便于检索。

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

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