[Python] How do I check to see if names from one file exist in another file?

Discussão em 'Python' iniciado por Stack, Maio 18, 2025.

  1. Stack

    Stack Membro Participativo

    Here is my code to see if names from modified_sn exist in modified_assigned_to_path (when I hit run, nothing happens, and I don't get any errors either. What am I doing wrong? Thanks in advance):

    sn_users_path = "C:\\Users\\karlj\\Downloads\\sn_users.csv"
    assigned_to_path = "C:\\Users\\karlj\\Downloads\\assigned_to.csv"
    modified_sn = "sn_users_path_modified.csv"
    modified_assigned_to_path = "assigned_to_path.csv"

    with open(sn_users_path, 'r') as sn:
    with open(modified_sn, 'w') as outfile:
    for user_line in sn:
    user_line = user_line.rstrip()
    if "@" in user_line and "." in user_line:
    users = user_line.split('@')
    users[0] = users[0].split(".")
    users[0] = " ".join(users[0])
    users[0] = users[0].title()
    if "," in user_line:
    users[1] = users[1].split(",")
    outfile.write(str(users) + '\n')
    else:
    user = user_line.split(",")
    outfile.write(str(user) + '\n')


    with open(modified_sn, 'r') as msn:
    for line in msn:
    line = eval(line)
    with open(modified_assigned_to_path) as map:
    for names_list in map:
    for name in names_list:
    if name == line[0]:
    print(name)


    Hi everyone, I'm somewhat of a Python beginner, maybe intermediate level, not sure what you would classify me as. Anyways, I have two files. I want to check if names in one csv file exist in another csv file. I took both original files and made some formatting changes by creating a new file and changing the strings into lists of strings, as well as maybe getting rid of some @ symbols. I am trying to see if the names in my modified_assigned_to_path file, which are all contained in one list, exist in any of the lists in my modified_sn list. Take a look at the formatting for reference for both these modified files for an easier visual understanding:

    modified_sn looks like (just a glimpse, but the rest of the file looks like this, all names formatted in proper case. TRUE or False just mean if that employee is still active or not):

    ['Wayne Stevenson', 'FALSE']

    ['Wayne Bradley', ['clarkwinterjv.com', 'TRUE']]

    modified_assigned_to_path looks like (just a glimpse, again, all names formatted with proper case:

    ['assigned_to_updated', 'Andy Fuhrmann', 'Gina Eaton', 'Lakeasha Williams', 'Chris Chow', 'Teha Herzi', 'Loida Maiselman']

    Continue reading...

Compartilhe esta Página