简体   繁体   English

我想通过 python 从文本文件中提取特定数据

[英]I want to pull specific data from a text file through python

I have a text file which contains data like given below:我有一个文本文件,其中包含如下所示的数据:

########### 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

I want (Number of events in Epg :-105 )我想要( Epg 中的事件数:-105

This data is from the text file.此数据来自文本文件。

If you are trying to get the rows that contain Epg :-105 then this is the code:如果您试图获取包含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  

You can then store those rows in a list instead of printing them然后您可以将这些行存储在列表中而不是打印它们

Your question doesn't clearly specify if you want the number of images data as well as the number of events data to be displayed.您的问题没有明确指定是否要显示图像数据的数量以及事件数据的数量。

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])

The above code has been used to determine the Number of events data.上面的代码已用于确定事件数据的数量。

To find both the number of events and the number of images data.查找事件数量和图像数据数量。 Use the snippet given below.使用下面给出的片段。

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])

The above code snippets use the knowledge of regular expressions.上面的代码片段使用了正则表达式的知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM