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")
这段代码的目标是在文本文件中搜索某个关键字,比如"和",然后将找到关键字的整行放入类型列表中(int ,串)。 int应该是行号和上面提到的整个行的字符串。
我还在编写行号 - 所以还没有问题。但问题是:输出是空的。即使我附加一个随机字符串而不是该行,我也不会得到任何结果。
如果我使用
if keyword.lower() in words:
print line
我得到所有想要的行,其中出现关键字。但我无法将其输入输出列表。
我尝试搜索的文本文件:http://www.gutenberg.org/cache/epub/844/pg844.txt
答案 0 :(得分:2)
请使用正则表达式。请参阅http://www.wolframalpha.com/input/?i=Where+am+I%3F的一些文档。替换每个字符/字符集都令人困惑。列表和wget Regex in Python
的使用看起来是正确的,但也许会考虑在for循环中调试你的.append()
变量,偶尔打印它以确保它的值是你想要的。
pyInProgress的答案对全局变量提出了一个很好的观点,但是如果没有测试它,我不相信如果使用line
返回变量而不是全局变量{{}} 1}}变量。如果您需要有关全局变量的更多信息,请参阅this StackOverflow post。
答案 1 :(得分:1)
循环遍历string.punctuation
以删除所有内容,然后重复行
import string, re
r = ','
def find_word(filepath, keyword):
output = []
with open(filepath, 'rb') as f:
data = f.read()
for x in list(string.punctuation):
if x != r:
data = data.replace(x, '')
data = re.sub(r',{2,}', r, data, re.M).splitlines()
for i, line in enumerate(data):
if keyword.lower() in line.lower().split(r):
output.append((i, line))
return output
print find_word('pg844.txt', 'and')
答案 2 :(得分:0)
由于output = list()
位于代码的顶层,并且不在函数内部,因此它被视为全局变量。
要编辑函数中的全局变量,必须先使用global
关键字。
示例:
gVar = 10
def editVar():
global gVar
gVar += 5
因此,要编辑函数output
中的变量find_word()
,您必须在为其分配值之前键入global output
。
它应该是这样的:
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
global output
if keyword.lower() in words:
output.append(line)
return output
将来,除非您绝对需要,否则请尽量远离全局变量。他们可能会变得混乱!