我是python的新手,正在为课程学习最终项目。我很难让程序在输入文件中搜索整个给定列表list1
并将正返回结果写入输出文件。我已经尝试过许多类似re.search
的事情,使用不同的循环制作函数,但问题总是相同的。它只会搜索list1[0]
中的内容。我不知道如何使它遍及整个列表。任何帮助将不胜感激,因为我确信这是我不熟悉或完全缺少的简单事物。
预期结果:程序将搜索列表中的单词(准确,但不区分大小写),它将找到的行复制到新的输出文件中,然后移至列表中的下一个单词,直到列表完成。
当前它只是将单词从列表中的第一个位置复制到文件中,并且无法继续前进。我不确定是否要复制整行,因为该行只是单词。
# Ask user for needed keywords or symbols
user_keywords = input("What keywords or special symbols would you like to search the provided file for?\n"
"Please separate each entry with a comma.\nIf you would like to just search for question marks,"
" please just type n.\n")
# Holding list, using comma as a way to separate the given words and symbols
list1 = list(user_keywords.split(','))
# Print list for user to see
print("You have entered the following keywords and/or special symbols: ", list1)
# Opens a new file for saving the results to.
print("Please list the path you would like the new file to save to. Example: C:\ Users \ NAME \Desktop\File name.")
outFileName = input()
outFile = open(outFileName,'w')
def filter_lines_by_keywords(lines_to_filter, key_words):
key_words_s = set(key_words)
return filter(lambda l: set(l.split()) & key_words_s, lines_to_filter)
# Opens the file under review.
with open(path1,'r+') as file1:
file1lines = file1.readlines()
res = filter_lines_by_keywords(lines_to_filter= file1lines, key_words= list1)
outFile.write(str(list(res)))
outFile.close()
如果我遗漏了代码中需要的任何内容,请告诉我,我想我已经全部包含了。我已经检查过类似的主题,但是找不到,但是如果存在,请告诉我。
先谢谢了。
答案 0 :(得分:1)
您可以使用上下文管理器同时打开两个文件,并使用any
运算符对输入的关键字进行测试,从而简化操作。
user_keywords = input("What keywords or special symbols would you like to search the provided file for?\n"
"Please separate each entry with a comma.\nIf you would like to just search for question marks,"
" please just type n.\n")
#string.split() already return a list so you dont have to turn it into a list again
list1 = [s.lower() for s in user_keywords.split(",")] #to achieve case insensitive
print("You have entered the following keywords and/or special symbols: ", list1)
print("Please list the path you would like the new file to save to. Example: C:\ Users \ NAME \Desktop\File name.")
outFileName = input()
with open(path1, "r") as f, open(outFileName ,"w") as w:
f = f.readlines()
for line in f:
if any(s in line.lower() for s in keywords): #test against all the items in keywords; return True if there is a hit
w.write(line)
答案 1 :(得分:0)
您可以尝试使用发电机
# Ask user for needed keywords or symbols
user_keywords = input("What keywords or special symbols would you like to search the provided file for?\n"
"Please separate each entry with a comma.\nIf you would like to just search for question marks,"
" please just type n.\n")
# Holding list, using comma as a way to separate the given words and symbols
list1 = list(user_keywords.split(','))
# Print list for user to see
print("You have entered the following keywords and/or special symbols: ", list1)
# Opens a new file for saving the results to.
print("Please list the path you would like the new file to save to. Example: C:\ Users \ NAME \Desktop\File name.")
outFileName = input()
outFile = open(outFileName,'w')
added_lines = []
def filter_lines_by_keywords(lines_to_filter, key_words):
global added_lines
for line in lines_to_filter:
input_words = line.rstrip().lower().split(' ')
for word in key_words:
if word.lower() in input_words and line not in added_lines:
added_lines.append(line)
yield line
# Opens the file under review.
with open(path1,'r+') as file1:
file1lines = file1.readlines()
res = filter_lines_by_keywords(lines_to_filter= file1lines, key_words= list1)
for i in res:
outFile.write(str(i))
outFile.close()
如果您不想匹配标点符号,则也必须将其删除。