【发布时间】:2016-01-19 12:34:04
【问题描述】:
r = ","
x = ""
output = list()
import string
def find_word(filepath,keyword):
doc = open(filepath, 'r')
for line in doc:
#Remove all the unneccessary characters
line = line.replace("'", r)
line = line.replace('`', r)
line = line.replace('[', r)
line = line.replace(']', r)
line = line.replace('{', r)
line = line.replace('}', r)
line = line.replace('(', r)
line = line.replace(')', r)
line = line.replace(':', r)
line = line.replace('.', r)
line = line.replace('!', r)
line = line.replace('?', r)
line = line.replace('"', r)
line = line.replace(';', r)
line = line.replace(' ', r)
line = line.replace(',,', r)
line = line.replace(',,,', r)
line = line.replace(',,,,', r)
line = line.replace(',,,,,', r)
line = line.replace(',,,,,,', r)
line = line.replace(',,,,,,,', r)
line = line.replace('#', r)
line = line.replace('*', r)
line = line.replace('**', r)
line = line.replace('***', r)
#Make the line lowercase
line = line.lower()
#Split the line after every r (comma) and name the result "word"
words = line.split(r)
#if the keyword (also in lowercase form) appears in the before created words list
#then append the list output by the whole line in which the keyword appears
if keyword.lower() in words:
output.append(line)
return output
print find_word("pg844.txt","and")
这段代码的目标是在一个文本文件中搜索某个关键字,比如“and”,然后将找到该关键字的整行放入一个类型为 (int,string) 的列表中。 int 应该是行号,字符串是上面提到的其余整行。
我仍在处理行号问题 - 所以对此没有任何疑问。但问题是:输出为空。即使我附加一个随机字符串而不是行,我也没有得到任何结果。
如果我使用
if keyword.lower() in words:
print line
我得到了所有需要的行,其中出现了关键字。但我就是无法将它放入输出列表中。
我试图搜索的文本文件:http://www.gutenberg.org/cache/epub/844/pg844.txt
【问题讨论】:
-
你是怎么调用函数的?
-
抱歉,我错过了最后一段代码。我编辑了原始帖子。
-
在文本文件中搜索某个关键字 - 有很多代码要做
for line_num, line in enumerate(open('filename')): if keyword.lower in line: output.append((line_num, line)) -
@TessellatingHeckler:
if keyword.lower() in line.lower() -
@TessellatingHeckler:另外,
'and' in 'band,hand'给出的结果与'and' in 'band,hand'.split(',')不同。拆分允许匹配仅在整个单词上。