我有一个名为test.txt
的文件。文件test.txt
内写着:
“画出我的意见绝对归来。否则因此,性行为确实是无情的东西。某些人因此而露出如此友善。为庆祝估计卓越而做。来或者生活在她的同性恋生活中。否则女儿满足的传达尝试没有。还是一般的访客出席了一百个太兄弟胖子到来。朋友是天还是活泼的新人。“
我试图从这个文件中抓取两个单词,例如单词“celebrated”和“atemted”。 这是我做的:
word = raw_input("what do you want to search ")
for i in open('test.txt'):
if word in i.split():
我也试过这个:
word = raw_input("what do you want to search ").split()
for i in open('test.txt'):
if word[0] and word[1] in i.split():
但我无法得到我正在寻找的结果。因为如果我在raw_input
语句中仅提供一个输入,则会给出错误,因为第二个值word[1]
未设置。有没有不同的方法来搜索两个或更多的单词?
答案 0 :(得分:2)
if word[0] in i.split() and word[1] in i.split():
最好将i.split()
放入变量
i_split = i.split()
if word[0] in i_split and word[1] in i_split:
您也可以使用all
i_split = i.split()
if all(word[i] in i_split for i in (0,1)):
更好地摆脱i
i_split = i.split()
if all(w in i_split for w in word):
您可以通过设置i_split
一套
i_split = set(i.split())
if all(w in i_split for w in word):
现在使用文件的上下文管理器
word = raw_input("what do you want to search ").split()
with open('test.txt') as fin:
for line in fin:
line_split = set(line.split())
if all(w in line_split for w in word):
...
如果您的意思是搜索整个文件而不是逐行搜索
word = raw_input("what do you want to search ").split()
with open('test.txt') as fin:
fin_split = set(fin.split())
if all(w in fin_split for w in word):
...
答案 1 :(得分:0)
您可以使用以下功能:
def find_words_in_file(file_path, words):
with open(file_path) as f:
contents = f.read() # should only be used for small files
results = {word:True if word in contents else False for word in words}
return results
def get_user_words():
user_input = raw_input("provide some words then press enter:\n")
return user_input.split()
def main():
file_path = "your/file/path/here"
words = get_user_words()
return find_words_in_file(file_path, words)
例如,我在下面使用了相同的逻辑:
words = ['celebrated', 'atemted']
contents = """
Drawings me opinions returned absolute in.
Otherwise therefore sex did are unfeeling something.
Certain be ye amiable by exposed so. To celebrated estimating excellence do.
Coming either suffer living her gay theirs.
Furnished do otherwise daughters contented conveying attempted no.
Was yet general visitor present hundred too brother fat arrival.
Friend are day own either lively new.
"""
results = {word:True if word in contents else False for word in words}
print results
<强>结果:强>
>>>
{'celebrated': True, 'atemted': False}