Conditionally merge 2 text files using Python
Question:
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?
Answers:
This will work… I have already tested it.
import re
def match_line(line):
match = re.match(r"//POINTER #(d+).*?-sSTRING #d.*$", line)
if match:
return match.groups()[0]
return False
def extract_file1(file1, file2):
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):
for i, line in enumerate(file2):
number2 = match_line(line)
if number2 and number2 == number1:
temp2 = i + 3
while file2[temp2] != "":
lst.append(file2[temp2])
temp2 += 1
break
def get_lines(path):
return open(path, "rt", encoding="utf8").read().split("n")
def main(path1, path2, path3):
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)
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?
This will work… I have already tested it.
import re
def match_line(line):
match = re.match(r"//POINTER #(d+).*?-sSTRING #d.*$", line)
if match:
return match.groups()[0]
return False
def extract_file1(file1, file2):
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):
for i, line in enumerate(file2):
number2 = match_line(line)
if number2 and number2 == number1:
temp2 = i + 3
while file2[temp2] != "":
lst.append(file2[temp2])
temp2 += 1
break
def get_lines(path):
return open(path, "rt", encoding="utf8").read().split("n")
def main(path1, path2, path3):
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)