Home > Mobile >  Python compare data in one file with another and print value seperated my colon
Python compare data in one file with another and print value seperated my colon

Time:10-15

Basically I have two files, email.txt and users.txt. Here is an example as to how they look like:

email.txt:

[email protected]
[email protected]
[email protected]

users.txt:

[email protected]:1030072275212435458
[email protected]:731498530124070912
[email protected]:846414846449654846

I need to iterate through email.txt file and print the corresponding ID for each of them from users.txt. In this case, the output would be this:

846414846449654846
1030072275212435458
731498530124070912

This is what I did so far:

users = open("users.txt", 'r')
with open("email.txt", 'r') as f:
    for email in f:
        for i in users.readlines():
            if i.startswith(email[:-1]):
                ID = i.split(':')[1][:-1]
                print(ID)

The problem with this is, it prints the ID of only the first email in the email.txt file, which is 846414846449654846, and ignores the rest of the emails in the list. Any sort of help would be highly appreciated.

CodePudding user response:

The problem is that you are only reading the lines in the users file once. You need to seek back to the beginning of the file after each iteration so that you can read the lines again.

You could do this with the users file open outside the loop:

with open("users.txt") as users:
    with open("email.txt") as f:
        for email in f:
            users.seek(0)
            for i in users:
                if i.startswith(email[:-1]):
                    ID = i.split(':')[1][:-1]
                    print(ID)
                    break

CodePudding user response:

Try collecting the info in users.txt in a dictionary and printing out the values of the dictionary using the lines in email.txt as keys.

with open("users.txt", "r") as users, open("email.txt", 'r') as f:
    user_dict = dict([tuple(row.strip().split(":")) for row in users])
    for email in f:
        print(user_dict[email.strip()])

OUTPUT

846414846449654846
1030072275212435458
731498530124070912

CodePudding user response:

users = open("users.txt", 'r').read().split('\n')
emails= open("email.txt", 'r').read().split('\n')

for e in emails:
    for u  in users:
        if u.split(':')[0]==e:
            print(u.split(':')[1])

CodePudding user response:

with open("users.txt", 'r') as file:
    users = file.read().strip()
with open("email.txt", 'r') as file:
    for email in file.readlines():
        email=email.strip()
        i=users.find(email)
        if i == -1:
            continue
        i = i   len(email)   1
        j = users.find('\n',i)
        if j==-1:
            Id=users[i:]
        else:
            Id=users[i:j]
        print(Id)

CodePudding user response:

user_map = {}
with open("users.txt") as f:
    lines = f.readlines()
    for line in lines:
        _line = line.split(":")
        key = _line[0]
        value = _line[1]
        user_map[key]= value

with open("email.txt") as f:
    keys = f.readlines()
    for key in keys:
        print(user_map[key.strip()])
    

CodePudding user response:

You might need to read the file as list and print using list comprehension:

ID = [print(i.split(':')[1]) for f in open('email.txt').read().splitlines() 
      for i in open('users.txt').read().splitlines() if f in i]

# 846414846449654846
# 1030072275212435458
# 731498530124070912

CodePudding user response:

Best you do it in two steps.

In first step create a dictionary dct_users with email as key and id as value out of the data stored in lines of users.txt file:

with open("users.txt", 'r') as u: 
    dct_users = {}
    for email, id in [ line.split(':') for line in u if ':' in line]: 
        dct_users[email.strip()] = id.strip()

Then use in the second step the created dictionary to print the id of the user with the given email:

with open("email.txt", 'r') as f:
    for line in f:
        email = line.strip()
        if email:  
            id = dct_users[email]
            print(id)

The code above tolerates empty lines and spaces in the "users.txt" and "email.txt" files.

  • Related