[英]How to print something if a particular string is not present in a text file in Python?
Suppose my text file is something like: 假设我的文本文件是这样的:
OK/INFO - 1070083 - Using waited I/O for the index and data files.. OK / INFO-1070083-将等待的I / O用于索引和数据文件。
OK/INFO - 1006069 - Data cache size ==> [100000] Kbytes, [2380] data pages. OK / INFO-1006069-数据缓存大小==> [100000] KB,[2380]个数据页。
OK/INFO - 1006070 - Data file cache size ==> [0] Kbytes, [0] data file pages. OK / INFO-1006070-数据文件缓存大小==> [0] KB,[0]个数据文件页面。
OK/INFO - 1200551 - Allocated TRIGMAXMEMSIZE: [4096] Bytes. OK / INFO-1200551-已分配的TRIGMAXMEMSIZE:[4096]字节。 .
。
OK/INFO - 1007046 - Restructuring of Database [Finstmt] Succeeded. OK / INFO-1007046-数据库重组[Finstmt]成功。
OK/INFO - 1007067 - Total Restructure Elapsed Time : [8.36] seconds. OK / INFO-1007067-总重组所用时间:[8.36]秒。
OK/INFO - 1013273 - Database NA_PLN.Finstmt altered. OK / INFO-1013273-数据库NA_PLN.Finstmt已更改。
for line in inputFile: if 'Elapsed' in line: print 'Present' if 'Elapsed' not in line: print 'Not present'
But this gives not present for nearly all the lines except for the one in which the required string is present. 但是,除了存在所需字符串的那一行外,几乎所有行都不存在。
Is there any way by which I can check for presence and absence and print only once?? 有什么方法可以检查是否存在并且只打印一次?
If you want the check to be performed for the entire file and not line by line you can do it like this: 如果要对整个文件执行检查,而不是逐行检查,则可以执行以下操作:
lines_in_file = open(inputFile).readlines()
test = 'Present' if any('Elapsed' in line for line in lines_in_file) else 'Not present'
print(test)
You can read more about any
here . 你可以阅读更多有关
any
位置 。 Also note that any
is lazy meaning that it does not have to go through the whole lines_in_file
container. 还要注意,
any
是惰性的 ,这意味着它不必遍历整个lines_in_file
容器。 It will exit as soon as its predicate ( 'Elapsed' in line
in this case) evaluates to True
. 一旦其谓词(在本例中为
'Elapsed' in line
)评估为True
,它将退出。
When you loop through the file line by line, the following statements will be executed for every line. 当您逐行循环浏览文件时,将对每一行执行以下语句。 What you want is some code that can basically say
您想要的是一些基本上可以说的代码
if "Elapsed" in file:
print("Present")
else:
print("Not present")
Since in python the read() function reads in the file as a literal string, new line characters and all, you can implement this code as the following: 由于在python中,read()函数以文本字符串,换行符及所有字符的形式读取文件,因此您可以按以下方式实现此代码:
file = open("filepath.txt") #this is your file's path
text = file.read()
if "Elapsed" in text:
print("Present")
else:
print("Not present")
This saves you the trouble of looping over the file. 这省去了遍历文件的麻烦。
Did you know that loops can have an else
clause? 您知道循环可以包含
else
子句吗?
for line in inputFile:
if 'Elapsed' in line:
print 'Present'
break
else:
print 'Not present'
This will let you know if Elapsed appears at least once in your file, which is what i understand you want to achieve 这将让您知道“已逝”是否在文件中至少出现了一次,这是我理解的目标
The else clause will only be called if the loop is allowed to complete, that is if "Elapsed" isn't in your file 仅当允许循环完成时,即文件中没有“ Elapsed”时,才会调用else子句
This should work: 这应该工作:
present = False
for line in inputFile:
if 'Elapsed' in line:
present = True
break
if present:
print 'Present'
else:
print 'Not present'
Unless the file is very long, you can just read the entire file content into a string and test it. 除非文件很长,否则您可以将整个文件内容读入字符串并进行测试。
>>> from pathlib import Path
>>> 'Elapsed' in Path('filename.txt').read_text()
True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.