高效的文本文件搜索。 [英] efficient text file search.

查看:74
本文介绍了高效的文本文件搜索。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有更有效的方法在文本文件中查找字符串:


f = file(''somefile'')

for line在f:

如果''字符串''在行:

打印''发现''





BTW:

确实对于f中的行:读取一块线到内存或是否

多次调用f.readline()?


谢谢

amit

Is there a more efficient method to find a string in a text file then:

f=file(''somefile'')
for line in f:
if ''string'' in line:
print ''FOUND''

?

BTW:
does "for line in f: " read a block of line to te memory or is it
simply calls f.readline() many times?

thanks
amit

推荐答案



" noro" < am ****** @ gmail.comschreef in bericht

news:11 ********************** @ h48g2000cwc .googlegr oups.com ...

"noro" <am******@gmail.comschreef in bericht
news:11**********************@h48g2000cwc.googlegr oups.com...

有没有更有效的方法在文本文件中查找字符串:


f = file(''somefile'')

for line in f:

if''string''in line:

print ''发现''
Is there a more efficient method to find a string in a text file then:

f=file(''somefile'')
for line in f:
if ''string'' in line:
print ''FOUND''



是的,更高效的是:

grep( http://www.gnu.org/software/grep/


yes, more efficient would be:
grep (http://www.gnu.org/software/grep/)


:)


来自python ...


Luuk写道:
:)

via python...

Luuk wrote:

" noro < am ****** @ gmail.comschreef in bericht

news:11 ********************** @ h48g2000cwc .googlegr oups.com ...
"noro" <am******@gmail.comschreef in bericht
news:11**********************@h48g2000cwc.googlegr oups.com...

有没有更有效的方法在文本文件中查找字符串:


f = file(''somefile'')

for line in f:

if''string''in line:

print ''发现''
Is there a more efficient method to find a string in a text file then:

f=file(''somefile'')
for line in f:
if ''string'' in line:
print ''FOUND''




是的,更高效的是:

grep( http://www.gnu.org/software/grep/




" noro" < am ****** @ gmail.comschreef in bericht

news:11 ******************** @ h48g2000cwc.googlegrou ps.com ...

"noro" <am******@gmail.comschreef in bericht
news:11********************@h48g2000cwc.googlegrou ps.com...

:)


来自python ...


Luuk写道:
:)

via python...

Luuk wrote:

>" noro" < am ****** @ gmail.comschreef在bericht
新闻:11 ********************** @ h48g2000cwc.googleg roups。 com ...
>"noro" <am******@gmail.comschreef in bericht
news:11**********************@h48g2000cwc.googleg roups.com...

有没有更有效的方法在文本文件中查找字符串:


f = file (''somefile'')

for line in f:

if''string''in line:

print''FOUND' '
Is there a more efficient method to find a string in a text file then:

f=file(''somefile'')
for line in f:
if ''string'' in line:
print ''FOUND''



是的,更高效的是:
grep( http://www.gnu.org/software/grep/



好吧,一个更严肃的回答:


一些谷歌搜索转为以下内容。

http://www.amk.ca/python/2.1/


a ..面向行的文件I / O的速度已经提高,因为人们经常抱怨它缺乏速度,并且因为它通常被用作

一个简单的基准。因此,文件对象的readline()方法已被重写为更快。加速的确切数量将从
平台到平台不等,具体取决于C库的getc()速度有多慢,但

约为66%,并且在一些特定的操作系统上可能会快得多。 Tim Peters为这一变化做了很多基准测试和编码,

由comp.lang.python中的讨论激发。

文件对象的新模块和方法也是添加,由杰夫贡献

Epler。新方法xreadlines()类似于现有的xrange()

内置。 xreadlines()返回一个不透明的序列对象,它只支持迭代的
,在每次迭代时读取一行,但不将整个文件作为现有的readlines读入内存中( )方法呢。你会像这样使用



for sys.stdin.xreadlines()中的行:

#...做点什么对于每一行...

...

有关线路I / O更改的更全面讨论,请参阅python-dev摘要

于2001年1月1日至15日在 http:/ /www.amk.ca/python/dev/2001-01-1.html

ok, a more serious answer:

some googling turned op the following.
Second paragraph of chapter 14 of http://www.amk.ca/python/2.1/

a.. The speed of line-oriented file I/O has been improved because people
often complain about its lack of speed, and because it''s often been used as
a na?ve benchmark. The readline() method of file objects has therefore been
rewritten to be much faster. The exact amount of the speedup will vary from
platform to platform depending on how slow the C library''s getc() was, but
is around 66%, and potentially much faster on some particular operating
systems. Tim Peters did much of the benchmarking and coding for this change,
motivated by a discussion in comp.lang.python.
A new module and method for file objects was also added, contributed by Jeff
Epler. The new method, xreadlines(), is similar to the existing xrange()
built-in. xreadlines() returns an opaque sequence object that only supports
being iterated over, reading a line on every iteration but not reading the
entire file into memory as the existing readlines() method does. You''d use
it like this:
for line in sys.stdin.xreadlines():
# ... do something for each line ...
...
For a fuller discussion of the line I/O changes, see the python-dev summary
for January 1-15, 2001 at http://www.amk.ca/python/dev/2001-01-1.html.


这篇关于高效的文本文件搜索。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆