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

2024-03-29 04:56:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在关联一个帐号和我想要的输出文件的名称。我需要搜索一个文本文件之间的帐户123456789和'谢谢你的查询'抓取代码,并将其写入一个输出文件名为'你好.txt'. 你知道吗

我需要再次循环并提取'55555-55555'和'Thank you for your inquiry'之间的信息,然后将其写入名为'再见.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

输出应为:

你知道吗你好.txt你知道吗

12345-6789
some lines
Thank you for your inquiry

你知道吗再见.txt你知道吗

55555-55555
some lines
Thank you for your inquiry

Tags: 文件tointxtyounumberforyour
1条回答
网友
1楼 · 发布于 2024-03-29 04:56:56

也许像这样的方法会奏效:

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')

相关问题 更多 >