簡體   English   中英

Python:比較一個文本文件中的正則表達式模式與另一個文本文件中的行

[英]Python: Compare regex pattern in one text file against line in another

這適用於較小的文本文件,但不適用於較大的文本文件 (100,000行)如何優化大型文本文件? 對於fileA中的行,如果regexPattern == fileB中的行將fileA中的(整個)行寫入fileC。

import re

with open('fileC.txt', 'w') as outfile:
    with open('fileA.txt', 'rU') as infile1:
        for line1 in infile1:
            y = re.findall(r'^.+,.+,(.+\.[a-z]+$)', line1)
                with open('fileB.txt', 'rU') as infile2:
                    for line2 in infile2:
                        if line2.strip() == y[0]:
                            outfile.write(line1)

最直接的優化是只將fileB.txt讀入字符串緩沖區一次,然后將匹配表達式的測試應用於該字符串緩沖區。 您當前正在為fileA.txt每一行打開和讀取該文件一次。

似乎你的正則表達式選擇了匹配模式的整行,即它以^開頭並以$結尾。 在這種情況下,更完整的解決方案是使用readlines()fileA.txtfileB.txt到數組中,對這些數組進行排序,然后通過兩個計數器單次傳遞兩個文件,例如:

# Details regarding the treatment of duplicate lines are ignored
# for clarity of exposition.
rai = sorted([7,6,1,9,11,6])
raj = sorted([4,6,11,7])
i, j = 0, 0
while i < len(rai) and j < len(raj):
    if   rai[i] < raj[j]: i += 1
    elif rai[i] > raj[j]: j += 1
    else:
        # I used mod in lieu of testing for your regex
        # since you didnt supply data
        if mod(rai[i],2): print rai[i]
        i, j = i + 1, j + 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM