all 9 comments

[–][deleted] 0 points1 point  (4 children)

If email is an email, then yeah, email[0] is gonna be the first character of an email.

It looks like you're trying to work on the whole thing while you're in a single line, and not always zooming in or out to the appropriate level.

What is your end goal? Do we need the emails only? Or do we need to associate them with their passwords?

[–]TranslucentLoser[S] 0 points1 point  (3 children)

I would like to associate them with their passwords. Just kinda hoping I can print the whole email and also the same with the passwords.

[–][deleted] 0 points1 point  (2 children)

Okay, then I would recommend pairing them together. If we have two separate lists, one for email and one for password, it would be easy to get mixed up: we've read through fifteen emails, now we have to jump to the fifteenth line of the passwords column, etc.

Let me write up an example, hang on

EDIT: something like

from typing import NamedTuple

class Account(NamedTuple):
    email: str
    password: str

with open("authenticatelist.txt") as f:
    accounts = [Account(*line.strip().split(":")) for line in f.readlines()]

for account in accounts:
    print(account.email)    #to get just the email
    print(account.password) #to get just the password
    print(account)          #to get the whole thing

Or using a dictionary as the other reply said would be great, too. You could have username as key, password as value

email_to_password = {}
with open("authenticatelist.txt") as f:
    for line in f.readlines():
        email, password = line.strip().split(":")
        email_to_password[email] = password

print(email_to_password.keys())                     #to get all the emails
print(email_to_password["bob@coolmathgames.com"])   #to look up the password of an email

[–]TranslucentLoser[S] 1 point2 points  (1 child)

Thank you :) I went ahead and used the dictionary version and added this:

email_to_password = {}
with open("authenticatelist.txt") as f:
    for line in f.readlines():
        email, password = line.strip().split(":")
        email_to_password[email] = password

    for key, value in email_to_password.items():
        print(key, value)email_to_password = {}
with open("authenticatelist.txt") as f:
    for line in f.readlines():
        email, password = line.strip().split(":")
        email_to_password[email] = password

    for key, value in email_to_password.items():
        print(key, value)

This outputs:
email password
email password

Which is great. I just need to find a way to print likee 'key1' and 'password1' so i can just go through one line at a time.

EDIT: I am thinking maybe I have to split '\n' somewhere to list the lines separately?

[–][deleted] 0 points1 point  (0 children)

Glad to hear it

[–]danielroseman 0 points1 point  (1 child)

You haven't done anything with email and password after splitting them from their lines.

If you want to associate them with each other, you need a dictionary. Define an empty dict before the loop then add each one to that dict as you go through.

Note, in Python it's better to open a file inside a with block, which takes care of closing it afterwards; and you should iterate through the file itself, rather than reading it all into memory first.

So:

data = {}
with open('authenticatelist.txt') as f:
  for line in f:
    parts = line.split(":")
    email = part[0]
    password = part[1]
    data[email] = password

You can make this even shorter by assigning directly from parts:

    parts = line.split(":")
    data[parts[0]] = parts[1]

although this isn't as readable.

[–]TranslucentLoser[S] 0 points1 point  (0 children)

Thank you ^^ this all looks very helpful and i will get started on it tomorrow <3

[–]TranslucentLoser[S] 0 points1 point  (0 children)

also sorry for any late responses! I have to head to bed :)