我有一个文本文件,其中包含如下所示的数据:
########### PLURIMEDIA APP1 REPORT of Date 2020-03-17 10:55:43 #################
Number of events in EPG:- 105 for channel:- 229 for date:- 20200317
Number of events in EPG:- 38 for channel:- 526 for date:- 20200317
Number of events in EPG:- 105 for channel:- 229 for date:- 20200318
Number of events in EPG:- 46 for channel:- 526 for date:- 20200318
Number of events in EPG:- 128 for channel:- 229 for date:- 20200319
Number of events in EPG:- 46 for channel:- 526 for date:- 20200319
Number of events in EPG:- 128 for channel:- 229 for date:- 20200320
Number of events in EPG:- 46 for channel:- 526 for date:- 20200320
Number of events in EPG:- 102 for channel:- 229 for date:- 20200321
Number of events in EPG:- 26 for channel:- 526 for date:- 20200321
Number of events in EPG:- 103 for channel:- 229 for date:- 20200322
Number of events in EPG:- 27 for channel:- 526 for date:- 20200322
Number of events in EPG:- 128 for channel:- 229 for date:- 20200323
Number of events in EPG:- 46 for channel:- 526 for date:- 20200323
Number of events in EPG:- 24 for channel:- 229 for date:- 20200324
Number of events in EPG:- 8 for channel:- 526 for date:- 20200324
Number of images in EPG :- 112 for channel:- 229 for date:- 20200317
Number of images in EPG :- 10 for channel:- 526 for date:- 20200317
Number of images in EPG :- 109 for channel:- 229 for date:- 20200318
Number of images in EPG :- 11 for channel:- 526 for date:- 20200318
Number of images in EPG :- 132 for channel:- 229 for date:- 20200319
我想要( EPg中的事件数:-105 )
此数据来自文本文件。
答案 0 :(得分:0)
如果您尝试获取包含 Epg:-105 的行,则代码如下:
file1 = open('myfile.txt', 'r') #read the txt file
Lines = file1.readlines() # read line by line
for line in Lines:
if 'EPG:- 105' in line: # check for each line if it contains 'Epg :-105'
print(line.strip()) # do whatever you want to do with those lines
然后您可以将这些行存储在列表中,而不必打印它们
答案 1 :(得分:0)
您的问题并未明确指定是否要显示图像数据数量和事件数据数量。
import re
fh = open('file.txt')
for line in fh:
line = line.rstrip()
stuff = re.findall('Number of events in EPG:- [0-9]+', line)
if len(stuff) == 0:
continue
else:
print(stuff[0])
上面的代码已用于确定事件数数据。
同时查找事件数和图像数据数。 使用下面给出的代码段。
import re
fh = open('file.txt')
for line in fh:
line = line.rstrip()
stuff = re.findall('Number of events in EPG:- [0-9]+', line)
if len(stuff) == 0:
stuff = re.findall('Number of images in EPG :- [0-9]+', line)
if len(stuff) == 0:
continue
else:
print(stuff[0])
else:
print(stuff[0])
以上代码段使用了正则表达式的知识。