mza box mza box - 4 months ago 18
Python Question

Notepad++ or python - Compare two lists and change the original changed

I have this original list.

John:password123
Daved:Password123
Steve:Password123
Michael:Password123





The second list is a random of original list and with changed passwords.

Michael:p241d111
John:fcvbfdg122
Steve:pdPo134!


What I want is to change the original list with new password but same postion.

For example:

John:fcvbfdg122
Daved:Password123
Steve:pdPo134!
Michael:p241d111


How to do that in notepad++ or in python!

Answer

test.txt

John:password123
Daved:Password123
Steve:Password123
Michael:Password123

test2.txt

Michael:p241d111
John:fcvbfdg122
Steve:pdPo134!

script.py

lst = {}

with open("test.txt") as f:
    for line in f:
        split = line.split(":")
        lst[split[0]] = split[1]

lst2 = {}

with open("test2.txt") as f:
    for line in f:
        split = line.split(":")
        lst2[split[0]] = split[1]

final_lst = {}

for item in lst:
    final_lst[item] = lst2.get(item, None) or lst[item]

print(final_lst)

Here is a simple solution written with Python.

~/temp ❯❯❯ python3 test2.py                                                                                                                                                            ⏎
{'Michael': 'p241d111', 'Steve': 'pdPo134!', 'John': 'fcvbfdg122', 'Daved': 'Password123'}