【发布时间】:2017-09-10 05:45:08
【问题描述】:
我正在尝试编写一个可以在文档中搜索特定键的脚本。有时文档可能非常大,因为我想要一些可以真正快速高效并且使用尽可能少的内存的东西。
这是我的代码
Keys=('key1','key2','key3','key4','key5',...,'keyN')
LKeys=list(Keys)
with open('test.txt', 'r') as inF:
for (N,line) in enumerate(inF):
if any(x in line for x in LKeys):
for k in LKeys:
if k in line:
print N,k,line
print inF.next().strip()
# Depending of the key sometimes I need to print next N lines
#Or do something else with the line for example save the next element after the key in a dictionary or database
LKeyword.remove(k)
inF.close()
test.txt 可能看起来像这样。
sdjskjd key1 jdjdjjd : 4
sdjskjd key2 jdjdjjd: 3 hdhdhd:NaN
sdjskjd key4 jdjdjjd:
dfdgdfdfdffddfdf2t3h
dfdfdfdfdf5dfd3fhth21
dfdfdfdgghhgdhhghjh
.
.
.
sdjskjd keyN jdjdjjd : 1213.5678 Inz:Joe
例如:
Author Antoine de Saint-Exupéry
Original title Le Petit Prince
Translator (English editions)
Katherine Woods
T.V.F. Cuffe
Irene Testot-Ferry
Alan Wakeman
Richard Howard[1]
David Wilkinson
Illustrator Antoine de Saint-Exupéry
Cover artist Antoine de Saint-Exupéry
Country France
Language French
Publisher Reynal & Hitchcock (U.S.)
Gallimard (France)[2]
Publication date
September 1943 (U.S.: English & French)
(France, French, 1945)[2][Note 1]
Preceded by Pilote de guerre (1942)
Followed by Lettre à un otage (1944)
Author Jostein Gaarder
Original title Sofies verden
Country Norway
Language Norwegian
Genre Philosophical novel
Publisher Berkley Books, Farrar, Straus and Giroux (original hardcover), MacMillan (audio)
Publication date
1991
Published in English
1994
Media type Print (hardcover & paperback) and audiobook (English, unabridged CD & download)
Pages 518 pp
ISBN 978-1-85799-291-5
ISBN 978-1-4272-0087-7
ISBN 978-1-4272-0086-0
OCLC 246845141
LC Class MLCM 92/06829 (P)
Auteur Gabriel García Márquez
Pays Drapeau de la Colombie Colombie
Genre Roman
Réalisme magique
Version originale
Langue Espagnol
Titre Cien años de soledad
Éditeur Editorial Sudamericana
Lieu de parution Buenos Aires
Date de parution 1967
Version française
Traducteur Claude et Carmen Durand
Éditeur Éditions du Seuil
Lieu de parution Paris
Date de parution 1968
Couverture Élizabeth Butterworth
Nombre de pages 437
ISBN 202023811X
我读到,有时按顺序访问行的简单迭代是搜索和匹配大型文档的内存需求较少的方式,有时可能比正则表达式更快。
一些关于我的密钥的 cmets 通常按顺序出现在我的文档中,如果找到该密钥,它将不会再次出现,因为我将其从列表中删除,但是,在某些情况下并非所有密钥都出现在文档中,但如果找到 key2 然后 key4 很可能 key3 不在文档中。此外,键是不可变的,并且在文档中显示为准确的单词。
有没有更好、更高效、更简洁的代码结构方式?
【问题讨论】:
-
如果与 open 一起使用,则不需要关闭 InF ...
标签: python performance memory-management text-files