因此,该文件包含大约57,000个书名,作者姓名和一个ETEXT编号。我正在尝试解析该文件,以仅获取ETEXT编号。
文件是这样的:
TITLE and AUTHOR ETEXT NO.
Aspects of plant life; with special reference to the British flora, 56900
by Robert Lloyd Praeger
The Vicar of Morwenstow, by Sabine Baring-Gould 56899
[Subtitle: Being a Life of Robert Stephen Hawker, M.A.]
Raamatun tutkisteluja IV, mennessä Charles T. Russell 56898
[Subtitle: Harmagedonin taistelu]
[Language: Finnish]
Raamatun tutkisteluja III, mennessä Charles T. Russell 56897
[Subtitle: Tulkoon valtakuntasi]
[Language: Finnish]
Tom Thatcher's Fortune, by Horatio Alger, Jr. 56896
A Yankee Flier in the Far East, by Al Avery 56895
and George Rutherford Montgomery
[Illustrator: Paul Laune]
Nancy Brandon's Mystery, by Lillian Garis 56894
Nervous Ills, by Boris Sidis 56893
[Subtitle: Their Cause and Cure]
Pensées sans langage, par Francis Picabia 56892
[Language: French]
Helon's Pilgrimage to Jerusalem, Volume 2 of 2, by Frederick Strauss 56891
[Subtitle: A picture of Judaism, in the century
which preceded the advent of our Savior]
Fra Tommaso Campanella, Vol. 1, di Luigi Amabile 56890
[Subtitle: la sua congiura, i suoi processi e la sua pazzia]
[Language: Italian]
The Blue Star, by Fletcher Pratt 56889
Importanza e risultati degli incrociamenti in avicoltura, 56888
di Teodoro Pascal
[Language: Italian]
这就是我尝试过的:
def search_by_etext():
fhand = open('GUTINDEX.ALL')
print("Search by ETEXT:")
for line in fhand:
if not line.startswith(" [") and not line.startswith("~"):
if not line.startswith(" ") and not line.startswith("TITLE"):
words = line.rstrip()
words = line.lstrip()
words = words[-7:]
print (words)
search_by_etext()
好的代码大部分都可以工作。但是,对于某些行,它给了我部分标题或其他内容。像:这种output(),包含“ decott”,它是作者名称的一部分,不应在此处。2
为此:奥利弗·赫尔福德(Oliver Herford)56765的《害羞的地震》 [副标题:其他寓言和诗歌]
兰花之家和其他诗歌,乔治斯特林56764
义大利北部民族,爱丽丝·范西塔特·斯特雷特尔·卡尔56763和伦道夫·卡尔德科特
新西兰的野生动物。第1部分,哺乳动物,乔治·汤姆森(George M. Thomson)56762 [副标题:新西兰科学和艺术委员会,手册第2号]
《环球兄弟会》,第1卷,第13卷,第10期,1899年1月,作者:56761
三个城市:卢尔德,埃米尔·佐拉(ÉmileZola)56760 [语言:荷兰语]
另一个例子:
对于Rhandensche Boys,由Jan Lens 56702撰写[插图:Tjeerd Bottema] [语言:荷兰语]
女人党的故事,因内斯·海恩斯·欧文(Inez Haynes Irwin)56701
摩门教义朴实,查尔斯·W·彭罗斯(Charles W. Penrose)56700 [副标题:或者生命之树上的叶子]
玛丽·格兰特·布鲁斯56699 [布尔卡穆克的石斧] [插图作者:J. Macfarlane]
后先知,乔治·Q·加农56698 [副标题:约瑟·史密斯为年轻人撰写的历史]
在这里:生活]不应该在那里。以空格开头的行已被解析为:
if not line.startswith(" [") and not line.startswith("~"):
但是仍然我在输出结果中得到了这些偏离值。
可能是那些未被过滤掉的多余行以“”字符以外的空格开头,例如制表符。作为可能可行的最小更改,请尝试过滤掉以任何空格开头的行,而不是专门使用空格字符的行?
要检查一般的空格而不是空格,您需要使用正则表达式。尝试if not re.match(r'^\s', line) and ...
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句