如何在文本文件中搜索字符串?
- 2024-12-11 08:47:00
- admin 原创
- 125
问题描述:
我想检查一个字符串是否在文本文件中。如果在,则执行 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:
两个问题:
你的函数没有返回任何东西;没有明确返回任何内容的函数将返回 None (即假值)
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")