Python 正则表达式多行,查找以 00 开头但不是 20 的行

Python Regex Multiline, finds lines starting with 00, but not 20

我有一个 python 脚本(如下),它应该在文本文件中找到以 00 和 20 开头的行,然后将这些行输出到两个单独的文件,一个用于 00,一个用于 20 . 它在 output1 上工作得很好,但为 output2 生成一个空元组。我究竟做错了什么?文本文件中的行都是相同的,没有特殊字符,并且以 00 或 20 开头。

import sys
import re
import glob
import os

listfiles = glob.glob('*.txt')



def DataExtract(inputfilename):
    myfilename1 = open('00 extract ' + inputfilename,'w')
    myfilename2 = open('20 extract ' + inputfilename,'w')

    with open(inputfilename, 'r') as f:
         output1 = re.findall(r'^00.*', f.read(), re.MULTILINE)
         output2 = re.findall(r'^20.*', f.read(), re.MULTILINE)

    wout1 = "\n".join(output1)
    wout2 = "\n".join(output2)
    print (wout2)
    print (output2)
    myfilename1.write(wout1)
    myfilename2.write(wout2)
    myfilename1.close
    myfilename2.close


for n in listfiles:
    DataExtract(n)

请帮忙!谢谢。

当您第二次调用 f.read() 时,没有更多内容可读,因为第一个 f.read() 已经消耗了文件流。因此,如果您将文件读入一个变量然后使用它而不是 f.read(),您可能会解决这个问题,但是由于您正在处理文字文本,您也可以逐行读取文件并使用str.startswith() 检查:

def DataExtract(inputfilename):
    myfilename1 = open('00 extract ' + inputfilename,'w')
    myfilename2 = open('20 extract ' + inputfilename,'w')

    with open(inputfilename, 'r') as f:
        for line in f:
            if line.startswith('00'):
                myfilename1.write(line)
            elif line.startswith('20'):
                myfilename2.write(line)

    myfilename1.close()
    myfilename2.close()