Home > OS >  Conditionally merge 2 text files using Python
Conditionally merge 2 text files using Python

Time:08-28

I'm currently trying to merge the 2 following text files:

//POINTER #3 @ $3A2C - STRING #3 @ $3C85
#W32($3A2C)
//・同行人の死亡[NLINE]
//・パーティーの全滅[END-FE]


//POINTER #4 @ $3A30 - STRING #4 @ $3CAA
#W32($3A30)
//・パーティーの全滅[END-FE]
//POINTER #3 @ $3BAC - STRING #3 @ $3E17
#W32($3BAC)
・Follower dies[NLINE]
・All party members die[END-FE]


//POINTER #4 @ $3BB0 - STRING #4 @ $3E42
#W32($3BB0)
・All party members die[END-FE]

After merging it should look like this:

//POINTER #3 @ $3A2C - STRING #3 @ $3C85
#W32($3A2C)
//・同行人の死亡[NLINE]
//・パーティーの全滅[END-FE]
・Follower dies[NLINE]
・All party members die[END-FE]

//POINTER #4 @ $3A30 - STRING #4 @ $3CAA
#W32($3A30)
//・パーティーの全滅[END-FE]
・All party members die[END-FE]

Does anybody have some pointers/ideas on how to accomplish this using a Python script?

CodePudding user response:

This will work... I have already tested it.

import re

def match_line(line):
    """This function looks for the POINTER lines and extracts the number"""
    match = re.match(r"//POINTER #(\d).*?-\sSTRING #\d.*$", line)
    if match:
        return match.groups()[0]
    return False

def extract_file1(file1, file2):
    """This function extracts each text block from file1."""
    lst = []
    start = 0
    while start < len(file1):
        number1 = match_line(file1[start])
        if number1:
            temp1 = start
            while file1[temp1] != "":
                lst.append(file1[temp1])
                temp1  = 1
            extract_file2(file2, lst, number1)
            start = temp1
            lst.append("")
        else:
            start  = 1
    return lst

def extract_file2(file2, lst, number1):
    """This function looks for the text block that matches 
       from file 1 and then adds the adds its additional 
       lines to the end of the block
    """
    for i, line in enumerate(file2):
        number2 = match_line(line)
        if number2 and number2 == number1:
            temp2 = i   2
            while file2[temp2] != "":
                lst.append(file2[temp2])
                temp2  = 1
            break

def get_lines(path):
    """This function opens each of the files containing text."""
    return open(path, "rt", encoding="utf8").read().split("\n")

def main(path1, path2, path3):
    """This function is the starting point and writes the final 
       output to a new file
    """
    file1, file2 = list(map(get_lines,[path1, path2]))
    lst = extract_file1(file1, file2)
    with open(path3, "wt", encoding="utf8") as fd:
        fd.write("\n".join(lst))

if __name__ == "__main__":
    path1 = "./file1.txt"
    path2 = "./file2.txt"
    path3 = "./file3.txt"
    main(path1, path2, path3)
  • Related