- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
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 out - 然后运行上面的正则表达式脚本。
我让 os.system 运行良好 - 我看不到如何通过 pOpen 传递正则表达式匹配
**** 最后更新 *** *******
我称之为完成。我最终做的是:
最终结果是,读取一个 1000 万行的文件(打印出必要的项目)大约需要 65 秒,而现在大约需要 3.5 秒。我希望我能弄清楚如何传递 grep 而不是 os.system - 但也许它在 python 2.4 中没有得到很好的实现
最佳答案
我认为这里不需要使用正则表达式。并不是说它们有那么糟糕,但是如果您正在寻找这样一个特定的模式,那么使用正则表达式就有点过分了。尝试这样的事情:
def record(filename, pattern):
with open(filename) as file:
recording = False
for line in file:
if line.startswith(pattern):
yield line
recording = not recording
elif recording:
yield line
使用文件名和您的模式调用 record
会为您提供一个生成器对象,逐行生成。这在处理大文件时效果更好,因此您不必立即吞下它们。
然后打印您的行可以像这样工作 - 假设您的文件名为 example.txt
:
for rec in record(filename, '---- Alpha ---- Zeta'):
print rec
准确地说:record
生成器生成包含换行符的行,因此您可能希望将它们join
在一起而不需要任何额外的换行符:
print "".join(list(record(filename, '---- Alpha ---- Zeta')))
关于Python 通读文件直到匹配,读取直到下一个模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7421621/
我是一名优秀的程序员,十分优秀!