如何在文本文件中搜索字符串?


169

我想检查字符串是否在文本文件中。如果是,请执行X。如果不是,请执行Y。但是,True由于某些原因,此代码始终返回。谁能看到哪里出了问题?

def check():
    datafile = file('example.txt')
    found = False
    for line in datafile:
        if blabla in line:
            found = True
            break

check()
if True:
    print "true"
else:
    print "false"

Answers:


391

您一直得到的原因True已经给出,因此我只提供另一个建议:

如果文件不是太大,则可以将其读取为字符串,然后使用它(比读取和检查每行更容易,并且通常更快):

with open('example.txt') as f:
    if 'blabla' in f.read():
        print("true")

另一个技巧:通过使用mmap.mmap()创建使用基础文件的“字符串状”对象(而不是读取内存中的整个文件),可以减轻可能的内存问题:

import mmap

with open('example.txt') as f:
    s = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
    if s.find('blabla') != -1:
        print('true')

注意:在python 3中,mmap的行为类似于bytearray对象而不是字符串,因此,例如,查找的子序列也find()必须是bytes对象而不是字符串。s.find(b'blabla')

#!/usr/bin/env python3
import mmap

with open('example.txt', 'rb', 0) as file, \
     mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
    if s.find(b'blabla') != -1:
        print('true')

您还可以在mmap不区分大小写的搜索中使用正则表达式:if re.search(br'(?i)blabla', s):


84
这太棒了!这正是我喜欢stackoverflow的原因:它不仅为您提供解决方案,而且还为您提供了更好的选择。谢谢:)
HankSmackHood 2011年

1
第二个解决方案给出的结果与'blabla' in open('example.txt').read()我的python 2.7
xApple

1
奇怪,它确实可以使用s.find('blabla')(检查-1)。我可以发誓它也可以使用in...但是现在看来,它in仅适用于单个字符...
Steven

6
if 'blabla' in open('example.txt').read(): print "true"==> example.txt在这种情况下,如何关闭文件?

4
open通常应该用以下with语句封装:with open(file_name) as fl: return text in fl.read()
Ohad Schneider 2015年

27

如Jeffrey Said所述,您没有检查的值check()。此外,您的check()函数未返回任何内容。注意区别:

def check():
    with open('example.txt') as f:
        datafile = f.readlines()
    found = False  # This isn't really necessary
    for line in datafile:
        if blabla in line:
            # found = True # Not necessary
            return True
    return False  # Because you finished the search without finding

然后,您可以测试的输出check()

if check():
    print('True')
else:
    print('False')

22

这是使用find函数可能回答您问题的另一种方法,该函数为您提供了真正存在位置的字面数值

open('file', 'r').read().find('')

在查找中输入您要查找的单词并'file'代表您的文件名


11
if True:
    print "true"

这总是发生,因为True始终为True。

您想要这样的东西:

if check():
    print "true"
else:
    print "false"

祝好运!


我知道了,现在可以了。不过,对我来说似乎有些怪异,这意味着Python会说“模块是True,除非另有说明”。因此,如果我要创建一个空模块,那将永远是对的吗?有趣的:)
HankSmackHood 2011年

11
不,一点也不-与模块无关。您只是在检查True是否为true,这是真的。
Daniel Roseman

5

为此,我做了一些功能。它在输入文件中搜索单词,然后将其添加到输出文件中。

def searcher(outf, inf, string):
    with open(outf, 'a') as f1:
        if string in open(inf).read():
            f1.write(string)
  • outf是输出文件
  • inf是输入文件
  • 字符串当然是您希望找到并添加到outf的所需字符串。

4

您的check函数应返回found布尔值,并使用该值确定要打印的内容。

def check():
        datafile = file('example.txt')
        found = False
        for line in datafile:
            if blabla in line:
                found = True
                break

        return found

found = check()
if found:
    print "true"
else:
    print "false"

第二块也可以浓缩为:

if check():
    print "true"
else:
    print "false"

1
除您之外,以上所有答案都是错误的。我花了半天的时间来猜测他们验证的答案在完全错误的情况下发生了什么。只有您一个人为我工作

2

两个问题:

  1. 您的函数不返回任何内容。没有明确返回任何内容的函数将返回None(这是错误的)

  2. True始终为True-您无需检查函数的结果

def check(fname, txt):
    with open(fname) as dataf:
        return any(txt in line for line in dataf)

if check('example.txt', 'blabla'):
    print "true"
else:
    print "false"

2

如何在文件中搜索文本并返回在其中找到单词的文件路径。

import os
import re

class Searcher:
    def __init__(self, path, query):
        self.path   = path

        if self.path[-1] != '/':
            self.path += '/'

        self.path = self.path.replace('/', '\\')
        self.query  = query
        self.searched = {}

    def find(self):
        for root, dirs, files in os.walk( self.path ):
            for file in files:
                if re.match(r'.*?\.txt$', file) is not None:
                    if root[-1] != '\\':
                        root += '\\'           
                    f = open(root + file, 'rt')
                    txt = f.read()
                    f.close()

                    count = len( re.findall( self.query, txt ) )
                    if count > 0:
                        self.searched[root + file] = count

    def getResults(self):
        return self.searched

在Main()中

# -*- coding: UTF-8 -*-

import sys
from search import Searcher

path = 'c:\\temp\\'
search = 'search string'


if __name__ == '__main__':

    if len(sys.argv) == 3:
        # создаем объект поисковика и передаем ему аргументы
        Search = Searcher(sys.argv[1], sys.argv[2])
    else:
        Search = Searcher(path, search)

    # начать поиск
    Search.find()

    # получаем результат
    results = Search.getResults()

    # выводим результат
    print 'Found ', len(results), ' files:'

    for file, count in results.items():
        print 'File: ', file, ' Found entries:' , count

如果您对本主题的问题没有得到本问答的回答,请在右上角提出一个新问题。
Sumurai13年

1

发现=错误

def check():
    datafile = file('example.txt')
    for line in datafile:
        if blabla in line:
            found = True
            break
    return found

if check():
    print "true"
else:
    print "false"

1

如果用户要在给定的文本文件中搜索单词。

 fopen = open('logfile.txt',mode='r+')

  fread = fopen.readlines()

  x = input("Enter the search string: ")

  for line in fread:

      if x in line:

          print(line)

0
found = False
def check():
datafile = file('example.txt')
for line in datafile:
    if "blabla" in line:
        found = True
        break
return found

if check():
    print "found"
else:
    print "not found"
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.