如何高效将两个有序数字文件合并至一个文件

with open("a.txt") as f1, open("b.txt") as f2, open("c.txt", "w") as f3:
    a1 = f1.next()
    b1 = f2.next()
    while True:
        if int(a1.strip("\n")) > int(b1.strip("\n")):
            f3.write(b1)
            try:
                b1 = f2.next()
            except StopIteration:
                f3.write(a1)
                for line in f1:
                    f3.write(line)
                break
        elif int(a1.strip("\n")) == int(b1.strip("\n")):
            f3.write(a1)
            try:
                a1 = f1.next()
            except StopIteration:
                f3.write(b1)
                for line in f2:
                    f3.write(line)
                break
            try:
                b1 = f2.next()
            except StopIteration:
                f3.write(a1)
                for line in f1:
                    f3.write(line)
                break
        elif int(a1.strip("\n")) < int(b1.strip("\n")):
            f3.write(a1)
            try:
                a1 = f1.next()
            except StopIteration:
                f3.write(b1)
                for line in f2:
                    f3.write(line)
                break


你可能感兴趣的:(如何高效将两个有序数字文件合并至一个文件)