简体   繁体   English

在python中搜索文件中的字符串

[英]Search for a string from file in python

I am reading a file with a different string on each line. 我正在读取每行上带有不同字符串的文件。 I want to be able to search an input string for a substring that matches an entire line in the file and then save that substring so that it can be printed. 我希望能够在输入字符串中搜索与文件中的整行匹配的子字符串,然后保存该子字符串以便可以打印它。 This is what I have right now: 这就是我现在所拥有的:

wordsDoc = open('Database.doc', 'r', encoding='latin-1')
words = wordsDoc.read().lower()
matching = [string for string in words if string in op_text]

But this matches on each character. 但这匹配每个角色。 How would I do this properly? 我该怎么做呢?

Couple of comments: 几点评论:

First, using with to open a file is usually better: 首先,使用with打开文件通常更好:

with open('Database.doc', 'r', encoding='latin-1') as f:
    # closes the file automagically at the end of this block...

Second, there is no need to read in the whole file unless you are doing something with the file as a whole. 其次,除非您对整个文件执行某些操作,否则无需读取整个文件。 Since you are searching lines, deal with the lines one by one: 由于您正在搜索线条,因此逐个处理这些线条:

matches=[]
with open('Database.doc', 'r', encoding='latin-1') as f:
    for line in f:
        if string in line.lower():
             matches.append(line)

If you are trying to match the entire line: 如果您尝试匹配行:

matches=[]
with open('Database.doc', 'r', encoding='latin-1') as f:
    for line in f:
        if string == line.lower():
             matches.append(line)

Or, more Pythonically, with a list comprehension: 或者,更奇怪的是,使用列表理解:

with open('Database.doc', 'r', encoding='latin-1') as f:
    matches=[line for line in f if line.lower()==string]

etc... 等等...

This will create a list named "matching" containing all the lines in the file that exactly match the string in op_text , once lowercased. 这将创建一个名为“matching”的列表,其中包含文件中与op_text中的字符串完全匹配的所有行(一旦是小写的)。

with open('Database.doc', 'r', encoding='latin-1') as wordsDoc:
    matching = [line for line in wordsDoc if op_text == line.lower()]

I assume the idea is that there is some search phrase and if it is contained in any line from the file, you want to filter those lines out. 我认为这个想法是有一些搜索短语,如果它包含在文件的任何行中,你想要过滤掉这些行。

Try this, which will compare the lower cased version of the line, but will return the original line from the file if it contains the search_key . 试试这个,它将比较该行的较低版本,但如果它包含search_key将从文件返回原始行。

with open('somefile.doc') as f:
   matching = [line for line in f if search_key in line.lower()]

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

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