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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4940032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 18:16:00  来源:igfitidea点击:

How to search for a string in text files?

python

提问by HankSmackHood

I want to check if a string is in a text file. If it is, do X. If it's not, do Y. However, this code always returns Truefor some reason. Can anyone see what is wrong?

我想检查一个字符串是否在文本文件中。如果是,则执行 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"

采纳答案by Steven

The reason why you always got Truehas already been given, so I'll just offer another suggestion:

你总是得到的原因True已经给出,所以我再提供一个建议:

If your file is not too large, you can read it into a string, and just use that (easier and often faster than reading and checking line per line):

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

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

Another trick: you can alleviate the possible memory problems by using mmap.mmap()to create a "string-like" object that uses the underlying file (instead of reading the whole file in memory):

另一个技巧:您可以通过使用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')

NOTE: in python 3, mmaps behave like bytearrayobjects rather than strings, so the subsequence you look for with find()has to be a bytesobject rather than a string as well, eg. s.find(b'blabla'):

注意:在python 3中,mmaps的行为类似于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')

You could also use regular expressions on mmape.g., case-insensitive search: if re.search(br'(?i)blabla', s):

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

回答by Jeffrey Greenham

if True:
    print "true"

This always happens because True is always True.

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

You want something like this:

你想要这样的东西:

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

Good luck!

祝你好运!

回答by amccormack

As Jeffrey Said, you are not checking the value of check(). In addition, your check()function is not returning anything. Note the difference:

正如杰弗里说的那样,您没有检查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

Then you can test the output of check():

然后你可以测试输出check()

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

回答by Sam Dolan

Your checkfunction should return the foundboolean and use that to determine what to print.

您的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"

the second block could also be condensed to:

第二个块也可以浓缩为:

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

回答by karlcow

found = False

发现 = 错误

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"

回答by Hugh Bothwell

Two problems:

两个问题:

  1. Your function does not return anything; a function that does not explicitly return anything returns None (which is falsy)

  2. True is always True - you are not checking the result of your function

  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"

回答by amitnaruto

Here's another way to possibly answer your question using the find function which gives you a literal numerical value of where something truly is

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

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

in find write the word you want to find and 'file'stands for your file name

在 find 中写下您要查找的单词并'file'代表您的文件名

回答by amitnaruto

How to search the text in the file and Returns an file path in which the word is found (Как искать часть текста в файле и возвращять путь к файлу в котором это слово найдено)

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

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

In 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

回答by Coco

I made a little function for this purpose. It searches for a word in the input file and then adds it to the output file.

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

def searcher(outf, inf, string):
    with open(outf, 'a') as f1:
        if string in open(inf).read():
            f1.write(string)
  • outf is the output file
  • inf is the input file
  • string is of course, the desired string that you wish to find and add to outf.
  • outf 是输出文件
  • inf 是输入文件
  • string 当然是您希望查找并添加到 outf 的所需字符串。

回答by Harshan Gowda

If user wants to search for the word in given text file.

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

 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)