【发布时间】:2014-12-03 22:56:23
【问题描述】:
目前,我正在尝试在文本文件中搜索确切的单词/短语。我正在使用 Python 3.4
这是我目前的代码。
import re
def main():
fileName = input("Please input the file name").lower()
term = input("Please enter the search term").lower()
fileName = fileName + ".txt"
regex_search(fileName, term)
def regex_search(file,term):
source = open(file, 'r')
destination = open("new.txt", 'w')
lines = []
for line in source:
if re.search(term, line):
lines.append(line)
for line in lines:
destination.write(line)
source.close()
destination.close()
'''
def search(file, term): #This function doesn't work
source = open(file, 'r')
destination = open("new.txt", 'w')
lines = [line for line in source if term in line.split()]
for line in lines:
destination.write(line)
source.close()
destination.close()'''
main()
在我的函数 regex_search 中,我使用正则表达式来搜索特定的字符串。但是,我不知道如何搜索特定的短语。
在第二个函数搜索中,我将行拆分为一个列表并在其中搜索单词。但是,这将无法搜索特定的短语,因为我在 ['the','dog','walked'] 中搜索 ["dog walk"] 不会返回正确的行。
【问题讨论】:
-
如果您搜索“foo”并且文本中有“foobar”,这是否被视为匹配?如果您搜索“foo bar”,并且一行以“foo”结尾,下一行以“bar”开头,这是否被视为匹配?
-
您能否提供输入文件(或其内容)的示例以及感兴趣的短语?
-
@Brian Oakley 两者都没有
标签: python