循环浏览文本文件并拆分为多个输出文件

时间:2019-03-26 20:13:44

标签: python

我正在将一个帐号和我想要的输出文件的名称相关联。我需要在帐户123456789和“感谢您的查询”之间的文本文件中进行搜索,以获取该代码并将其写入名为“ Hello.txt”的输出文件中。

我需要再次循环并提取“ 55555-55555”和“谢谢您的询问”之间的信息,并将其写入名为“ Bye.txt”的输出文件中。当我运行此代码时,没有任何内容写入我的文件。

输入:

Account number: 123456789
Hi,
This is you bill. Please do not forget to include your account number on
your check.
If you have any further questions please feel free to contact me 1-800-325-
3232. Press
option 1 to reach my extension 1234.
Thank you for your inquiry

Account Number: 55555-55555
Hi,
This is you bill. Please do not forget to include your account number on
your check.
If you have any further questions please feel free to contact me 1-800-325-
3232. Press
option 1 to reach my extension 1234.
Thank you for your inquiry

我的脚本:

with open('SumBillRpt2019-2-27 Cyl 20.txt') as of:
    for line in of.read().split('\n'):
        for account, new_file in d.items():
            with open(new_file, 'w') as nf:
                if account in line:
                    writing = True
                if writing:
                    nf.write(line)
                    print(nf)
                if 'Thank you for your Inquiry' in line:
                    writing = False

输出应为:

hello.txt

12345-6789
some lines
Thank you for your inquiry

Bye.txt

55555-55555
some lines
Thank you for your inquiry

1 个答案:

答案 0 :(得分:0)

也许这样可以工作:

def to_file(in_file, start_key, end_key, out_file):
    with open(in_file) as fd:
        data = fd.readlines()

    start_index = 0
    while start_index < len(data) and start_key not in data[start_index]:
        start_index += 1

    if start_index == len(data):
        print(" start_key not found")
        return

    with open(out_file, 'w') as fdo:
        curr = start_index
        while curr < len(data) and end_key not in data[curr]:
            fdo.write(data[curr])
            curr += 1

        if end_key == len(data):
            print(" end_key not found")
            return
        fdo.write(data[curr])

另一种方法是改为使用re库:

def to_file(in_file, start_key, end_key, out_file):
    import re

    with open(in_file) as fd:
        data = fd.read()

    PATTERN = rf'{start_key}.*?{end_key}'

    try:
        result_found = re.findall(PATTERN, data, re.DOTALL)[0]
    except IndexError:
        print("Pattern not found")
        return

    with open(out_file, 'w') as fd:
        fd.write(result_found)

要调用上面的任何一个函数,请使用类似以下内容:

to_file('SumBill.txt', '123456789', 'thank you for your inquiry', 'hello.txt')
to_file('SumBill.txt', '55555-55555', 'Thank you for your inquiry', 'bye.txt')