简体   繁体   English

使用带有字符串的文本文件并在文件夹中的文件中搜索字符串

[英]Using a text file with strings and searching for the strings in files in folders

I'm working on searching for strings inside text files.我正在搜索文本文件中的字符串。 What I have is a CSV file with multiple lines of a single word.我拥有的是一个包含多行单个单词的 CSV 文件。 Now I need to search files in multiple folders and subfolders for the words in this CSV file.现在我需要在多个文件夹和子文件夹中的文件中搜索此 CSV 文件中的单词。 In the end I would like to dump out the results into a text file.最后,我想将结果转储到文本文件中。 The results should have the original word and the result file name that the string was found in. How do you loop through a CSV file with strings while searching files for with these strings?结果应该包含原始单词和找到字符串的结果文件名。如何在使用这些字符串搜索文件时循环遍历包含字符串的 CSV 文件? I've only come across individual Python programs that will search for one string in a folder and then print out the results.我只遇到过单独的 Python 程序,它们会在文件夹中搜索一个字符串,然后打印出结果。 I've modified one of these to print to a file but am having trouble looping through a CSV search string file.我已经修改了其中之一以打印到文件,但在遍历 CSV 搜索字符串文件时遇到问题。

I suggest the following approach: read the CSV file and create the list of search words.我建议采用以下方法:读取 CSV 文件并创建搜索词列表。 Then create a regular expression out of them, matching any of these words:然后用它们创建一个正则表达式,匹配这些单词中的任何一个:

regexp = re.compile( '(' + '|'.join(words) + ')' )

Then go through the files using os.walk and apply the regexp to them using re.search .然后使用os.walk浏览文件并使用re.search将正则表达式应用于它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM