在文件中搜索单词 [英] searching for words in a file

查看:65
本文介绍了在文件中搜索单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有大量文件,我将如何在所有文件中搜索特定单词,然后让系统打印出那些具有我要查找的单词的文件中的所有行。


如果我想要另外一个程序来搜索我选择的两个单词然后只打印出那些包含这两个单词的文件中的行,那么会有什么不同。

If I have a large number of files how would I search for a particular word in all of the files then have the system print out all the lines in those files that have the word that I am looking for.

What would have to be different if I wanted to do another program that would search for two words of my choice and then only print out the lines in those files that contain both those words.

推荐答案


如果我有大量文件,我将如何搜索所有文件中的特定单词然后让系统打印出那些包含我正在寻找的单词的文件中的所有行。


如果我想要另外一个程序来搜索我选择的两个单词然后只打印出那些包含这两个单词的文件中的行,那么会有什么不同。
If I have a large number of files how would I search for a particular word in all of the files then have the system print out all the lines in those files that have the word that I am looking for.

What would have to be different if I wanted to do another program that would search for two words of my choice and then only print out the lines in those files that contain both those words.



1)使用glob模块查看目录树中的文件。打开每个文件并将其读入缓冲区。关闭文件。使用re模块创建Regular Expression对象。如果你的正则表达式匹配,打印glob给你的(文件名?)(或缓冲区,如果你的意思是打印文件)。


2)建立你的带变量的正则表达式。

1) Use the glob module to look through directory trees for the files. Open each file and read it into a buffer. Close the file. Use the re module to create a Regular Expression object. Print the (file name?) that glob just gave you (or the buffer if you meant "print the file") if your Regular Expression gets a match.

2) build your Regular Expression with variables.


我不确定你对glob模块或其他什么意思?我想要做的是编写一个脚本,搜索60个文件中的某个单词,然后打印出所有包含该单词的行,然后我也想在另一个程序中编写一个给出两个单词的脚本,搜索这些单词,然后打印出那些包含这两个单词的文件中的行。
I am not sure what you mean by the glob module or anything? What I am trying to do is to write a script that would search 60 files for a certain word and then print out all the lines that have that word, then I want to also in a different program write a script that given two words, would search for those words then print out the lines in those files that contain both those words.


我所拥有的是关于名为files的文件夹中的70个不同文件。在该文件夹中,如果在像word这样的程序中打开,则每个文件都包含一个句子列表。所以我需要做的是能够输入一个搜索词,然后让程序返回包含该词的所有文件中的所有句子。
What I have are about 70 different files in a folder called "files" In that folder each file if opened in a program like word contains a list of sentences. So what I need to do is to be able to type in a search word and then have the program return all the sentences out of all the files that contain that word.


这篇关于在文件中搜索单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆