Home > Software design >  How to join two large disk .txt files on ID number if they are separated by newline character?
How to join two large disk .txt files on ID number if they are separated by newline character?

Time:10-08

I need to get this result:

Example input: (two files)

Adam 1234

John 4321

Anderson 4321

Smith 1234

Expected output:

Adam Smith 1234

John Anderson 4321

My code for now works like this, it combines two .txt files... but I can't figure out how to combine them on the same ID?

    new = []
    with open('full.txt', 'w') as new_file:
        with open('1.txt') as fp_first_name:
            with open('2.txt') as fp_last_name:
                for line1 in fp_first_name:
                    for line2 in fp_last_name:
                        new_file.write(line1[:len(line1[-5:])]   line2)

I've tried like

if line2[-5:] == line1[-5:]:
new_file.write(line1[:len(line1[-5:])]   line2)

but this of course only takes first line of each "line2" and "line1", it doesn't go through a whole .txt file which it needs to do.

CodePudding user response:

you can split the line and have the name and number for each line then compare and combine in case it is a match:

name1, number1 = line1.split(" ")
name2, number2 = line2.split(" ")
if number2 == number1:
    new_file.write(f"{name1} {name2} {number2}")

new code:

new = []
with open('full.txt', 'w') as new_file:
    with open('file1.txt') as fp_first_name:
        for line1 in fp_first_name:
            with open('file2.txt') as fp_last_name:
                for line2 in fp_last_name:
#                    print(f"L1: {line1}L2: {line2}")
                    name1, number1 = line1.split(" ")
                    name2, number2 = line2.split(" ")
#                    print(f"what {name1} {number1} {name2} {number2}")
                    if number2.strip() == number1.strip():
                        new_file.write(f"{name1} {name2} {number2}")
  • Related