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

2024-12-11 08:47:00
admin
原创
125
摘要:问题描述:我想检查一个字符串是否在文本文件中。如果在,则执行 X。如果不在,则执行 Y。但是,True由于某种原因,此代码总是返回。有人能看出哪里出了问题吗?def check(): datafile = file('example.txt') found = False for li...

问题描述:

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

解决方案 1:

您总是得到的原因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, \n     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):

解决方案 2:

正如 Jeffrey 所说,您没有检查 的值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')

解决方案 3:

这是另一种可能回答您问题的方法,使用 find 函数,它可以为您提供某个事物真正所在位置的文字数值

open('file_to_search_into', 'r').read().find('searched_string')

解决方案 4:

if True:
    print "true"

这种情况总是会发生,因为 True 始终是 True。

你想要这样的东西:

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

祝你好运!

解决方案 5:

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

def searcher(outf, inf, string):
    with open(outf, 'a') as f1:
        if string in open(inf).read():
            f1.write(string)
  • outf 是输出文件

  • inf 是输入文件

  • 字符串当然是您希望找到并添加到 outf 的字符串。

解决方案 6:

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

解决方案 7:

两个问题:

  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"

解决方案 8:

如何搜索文件中的文本并返回找到该单词的文件路径 (Как искать часть текста в файле и возвращять путь к файлу в котором это слово найдено)

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

解决方案 9:

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

 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)

解决方案 10:

发现 = 错误

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"

解决方案 11:

如果您正在主动更新/与文件中的信息交互,请记住以下事项。i)读入的文件将不包含您可能正在搜索的附加数据。

   my_file = open('path/to/my_file', a+)# in append plus mode (read and write)
   my_file.write("foo") #
   if my_file.read().find("foo") !=-1: # returns -1
       print("this condition is not met")
   # you would have to read the file in again
   my_file = open('path/to/my_file', a+)
   #now the appended text is in active memory

ii)readlines 是可迭代的,将在以下情况下使用:

# I was merging text from two files
original_exif = open(my_file)
donor_file - open(my_file)
# this list is full
original_lines = original_exif.readlines()
for line in original_exif:
    print(line)
    temp_exif.write(line)
    print(f"where are we in the temp_exif:
 {temp_exif.tell()}")



print(f"original lines full list
 {original_lines}")
original_lines = original_exif.readlines()
print(f"original lines declared after being iterated through and the list is now empty
 {original_lines}")

我希望可以在这里发帖。它不是直接回答搜索帖子,但这些细节可能会有所不同,它们都让我在尝试使用脚本合并来自文件组合的文本时遇到了问题。

解决方案 12:

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"

解决方案 13:

这是另一个。获取绝对文件路径和给定字符串并将其传递给 word_find(),在 enumerate() 方法中对给定文件使用 readlines() 方法,该方法在逐行遍历时给出可迭代计数,最后给出匹配字符串的行以及给定的行号。干杯。

  def word_find(file, word):
    with open(file, 'r') as target_file:
        for num, line in enumerate(target_file.readlines(), 1):
            if str(word) in line:
                print(f'<Line {num}> {line}')
            else:
                print(f'> {word} not found.')


  if __name__ == '__main__':
      file_to_process = '/path/to/file'
      string_to_find = input()
      word_find(file_to_process, string_to_find)

解决方案 14:

“found” 需要在函数中创建为全局变量,因为“if else”语句超出了函数范围。您也不需要使用“break”来中断循环代码。下面的代码应该可以找出文本文件是否包含所需的字符串。

with open('text_text.txt') as f:
    datafile = f.readlines()


def check():
    global found
    found = False
    for line in datafile:
        if 'the' in line:
            found = True


check()

if found == True:
    print("True")
else:
    print("False")
相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   1565  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1354  
  信创国产芯片作为信息技术创新的核心领域,对于推动国家自主可控生态建设具有至关重要的意义。在全球科技竞争日益激烈的背景下,实现信息技术的自主可控,摆脱对国外技术的依赖,已成为保障国家信息安全和产业可持续发展的关键。国产芯片作为信创产业的基石,其发展水平直接影响着整个信创生态的构建与完善。通过不断提升国产芯片的技术实力、产...
国产信创系统   21  
  信创生态建设旨在实现信息技术领域的自主创新和安全可控,涵盖了从硬件到软件的全产业链。随着数字化转型的加速,信创生态建设的重要性日益凸显,它不仅关乎国家的信息安全,更是推动产业升级和经济高质量发展的关键力量。然而,在推进信创生态建设的过程中,面临着诸多复杂且严峻的挑战,需要深入剖析并寻找切实可行的解决方案。技术创新难题技...
信创操作系统   27  
  信创产业作为国家信息技术创新发展的重要领域,对于保障国家信息安全、推动产业升级具有关键意义。而国产芯片作为信创产业的核心基石,其研发进展备受关注。在信创国产芯片的研发征程中,面临着诸多复杂且艰巨的难点,这些难点犹如一道道关卡,阻碍着国产芯片的快速发展。然而,科研人员和相关企业并未退缩,积极探索并提出了一系列切实可行的解...
国产化替代产品目录   28  
热门文章
项目管理软件有哪些?
云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用