Python 2.4.3
我需要读取一些文件(可能会非常大,达到10GB)。我的需求是查找文件中的某个模式。当找到该模式时,打印出该行以及其后的每一行,直到找到另一个模式为止。然后继续查找文件,直到下一个模式匹配。
例如,文件内容如下:
---- Alpha ---- Zeta
...(text lines)
---- Bravo ---- Delta
...(text lines)
等到匹配到 ---- Alpha ---- Zeta 时,它应该打印出来 ---- Alpha ---- Zeta 并且每一行直到遇到 ---- Bravo ---- Delta(或任何其他不是 ---- Alpha ---- Zeta的内容),然后跳过此内容并继续读取,直到再次匹配到 ---- Alpha ---- Zeta。
以下内容与我想要的匹配 - 但只打印匹配行 - 而不是后面的文本。
你有什么想法吗?
import re
fh = open('text.txt', 'r')
re1='(-)' # Any Single Character 1
re2='(-)' # Any Single Character 2
re3='(-)' # Any Single Character 3
re4='(-)' # Any Single Character 4
re5='( )' # White Space 1
re6='(Alpha)' # Word 1
re6a='((?:[a-z][a-z]+))' # Word 1 alternate
re7='( )' # White Space 2
re8='(-)' # Any Single Character 5
re9='(-)' # Any Single Character 6
re10='(-)' # Any Single Character 7
re11='(-)' # Any Single Character 8
re12='(\\s+)' # White Space 3
re13='(Zeta)' # Word 2
re13a='((?:[a-z][a-z]+))' # Word 2 alternate
rg = re.compile(re1+re2+re3+re4+re5+re6+re7+re8+re9+re10+re11+re12+re13,re.IGNORECASE|re.DOTALL)
rga = re.compile(re1+re2+re3+re4+re5+re6a+re7+re8+re9+re10+re11+re12+re13a,re.IGNORECASE|re.DOTALL)
for line in fh:
if re.match(rg, line):
print line
fh.next()
while not re.match(rga, line):
print fh.next()
fh.close()
以及我的示例文本文件。
---- Pappa ---- Oscar
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris eleifend imperdiet
lacus quis imperdiet. Nulla erat neque, laoreet vel fermentum a, dapibus in sem.
Maecenas elementum nisi nec neque pellentesque ac rutrum urna cursus. Nam non purus
sit amet dolor fringilla venenatis. Integer augue neque, scelerisque ac dictum at,
venenatis elementum libero. Etiam nec ante in augue porttitor laoreet. Aenean ultrices
pellentesque erat, id porta nulla vehicula id. Cras eu ante nec diam dapibus hendrerit
in ac diam. Vivamus velit erat, tincidunt id tempus vitae, tempor vel leo. Donec
aliquam nibh mi, non dignissim justo.
---- Alpha ---- Zeta
Sed molestie tincidunt euismod. Morbi ultrices diam a nibh varius congue. Nulla velit
erat, luctus ac ornare vitae, pharetra quis felis. Sed diam orci, accumsan eget
commodo eu, posuere sed mi. Phasellus non leo erat. Mauris turpis ipsum, mollis sed
ismod nec, aliquam non quam. Vestibulum sem eros, euismod ut pharetra sit amet,
dignissim eget leo.
---- Charley ---- Oscar
Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
Aliquam commodo, metus at vulputate hendrerit, dui justo tempor dui, at posuere
ante vitae lorem. Fusce rutrum nibh a erat condimentum laoreet. Nullam eu hendrerit
sapien. Suspendisse id lobortis urna. Maecenas ut suscipit nisi. Proin et metus at
urna euismod sollicitudin eu at mi. Aliquam ac egestas magna. Quisque ac vestibulum
lectus. Duis ac libero magna, et volutpat odio. Cras mollis tincidunt nibh vel rutrum.
Curabitur fringilla, ante eget scelerisque rhoncus, libero nisl porta leo, ac
vulputate mi erat vitae felis. Praesent auctor fringilla rutrum. Aenean sapien ligula,
imperdiet sodales ullamcorper ut, vulputate at enim.
---- Bravo ---- Delta
Donec cursus tincidunt pellentesque. Maecenas neque nisi, dignissim ac aliquet ac,
vestibulum ut tortor. Pellentesque habitant morbi tristique senectus et netus et
malesuada fames ac turpis egestas. Aenean ullamcorper dapibus accumsan. Aenean eros
tortor, ultrices at adipiscing sed, lobortis nec dolor. Fusce eros ligula, posuere
quis porta nec, rhoncus et leo. Curabitur turpis nunc, accumsan posuere pulvinar eget,
sollicitudin eget ipsum. Sed a nibh ac est porta sollicitudin. Pellentesque ut urna ut
risus pharetra mollis tincidunt sit amet sapien. Sed semper sollicitudin eros quis
pellentesque. Curabitur ac metus lorem, ac malesuada ipsum. Nulla turpis erat, congue
eu gravida nec, egestas id nisi. Praesent tellus ligula, pretium vitae ullamcorper
vitae, gravida eu ipsum. Cras sed erat ligula.
---- Alpha ---- Zeta
Cras id condimentum lectus. Sed sit amet odio eros, ut mollis sapien. Etiam varius
tincidunt quam nec mattis. Nunc eu varius magna. Maecenas id ante nisl. Cras sed augue
ipsum, non mollis velit. Fusce eu urna id justo sagittis laoreet non id urna. Nullam
venenatis tincidunt gravida. Proin mattis est sit amet dolor malesuada sagittis.
Curabitur in lacus rhoncus mi posuere ullamcorper. Phasellus eget odio libero, ut
lacinia orci. Pellentesque iaculis, ligula at varius vulputate, arcu leo dignissim
massa, non adipiscing lectus magna nec dolor. Quisque in libero nec orci vestibulum
dapibus. Nulla turpis massa, varius quis gravida eu, bibendum et nisl. Fusce tincidunt
laoreet elit, sed egestas diam pharetra eget. Maecenas lacus velit, egestas nec tempor
eget, hendrerit et massa.
+++++++++++++++++++++ 更新 ++++++++++++++++++++++++++++++++
下面的代码是有效的,它会匹配头部类型行 - 输出该行及其后每一行直到下一个头部类型模式 - 如果不匹配则跳过直到下一个头部类型模式。
唯一的问题是 - 它非常非常慢。对于10m行而言,需要大约一分钟才能完成。
re1='(-)' # Any Single Character 1
re2='(-)' # Any Single Character 2
re3='(-)' # Any Single Character 3
re4='(-)' # Any Single Character 4
re5='( )' # White Space 1
re6='(Alpha)' # Word 1
re6a='((?:[a-z][a-z]+))' # Word 1 alternate
re7='( )' # White Space 2
re8='(-)' # Any Single Character 5
re9='(-)' # Any Single Character 6
re10='(-)' # Any Single Character 7
re11='(-)' # Any Single Character 8
re12='(\\s+)' # White Space 3
re13='(Zeta)' # Word 2
re13a='((?:[a-z][a-z]+))' # Word 2 alternate
rg = re.compile(re1+re2+re3+re4+re5+re6+re7+re8+re9+re10+re11+re12+re13,re.IGNORECASE|re.DOTALL)
rga = re.compile(re1+re2+re3+re4+re5+re6a+re7+re8+re9+re10+re11+re12+re13a,re.IGNORECASE|re.DOTALL)
linestop = 0
fh = open('test.txt', 'r')
for line in fh:
if linestop == 0:
if re.match(rg, line):
print line
linestop = 1
else:
if re.match(rga, line):
linestop = 0
else:
print line
fh.close()
如果我先加上一个grep部分,我认为这会极大地加快速度。即grep出-然后运行上面的正则表达式脚本。
我成功地使用了os.system - 我不知道如何通过pOpen传递正则表达式匹配项。
**** 最终更新 **********
我将其称为已完成。我最终做的是:
- 使用os.system对文件进行grep,并将结果写出。
- 读取文件并使用上述的re.match - 仅打印必要的项目。
最终结果是,从读取1000万行文件并打印必要项目需要约65秒的时间,缩短到约3.5秒。我希望我能够找出除了os.system之外传递grep的方法 - 但也许在Python 2.4中它没有被很好地实现。
sed -ne '/foo/,/bar/p'
或perl -ne 'print if /foo/ .. /bar/'
,因为你需要一个简单的翻转操作符吗?请注意,..
和...
是不同的,因为..
在LHS变为true时立即测试RHS,而...
则等到下一次。这就像++i
和i--
之间的区别一样,它们是两个更基本的运算符,但你仍然必须从头开始重新编写它们。这个操作的整体应用是什么? - tchrist