[英]how to print specific line from file where it matches input string from large text file?
Here is my code my aim is to print a specific whole line from large text file where string equal to "SAMPLE TYPE" when i tried its printing in the object how to print specified line from whole file 这是我的代码,我的目标是从大型文本文件中打印特定的整行,当我尝试在对象中进行打印时,字符串等于“ SAMPLE TYPE”的字符串如何从整个文件中打印指定的行
import re
target = open("debug.txt", "r")
for line in target:
search=re.search("[SAMPLE TYPE]",line)
v1=search
print v1
Gives output of: 提供以下内容的输出:
None
None
None
None
None
<_sre.SRE_Match object at 0x7f870b7d5578>in this place instead of this i need to print the specifed line and i dont want the lines which doesnt contain specifed string.
My input file data: 我的输入文件数据:
HLS| 04/14/16 17:56:58:933 |hls_tsr_module.cpp|ReceiveData |418 |DEBUG: Data copied to TSD from TSR is 0
HLS| 04/14/16 17:56:58:933 |mpm_h264.cpp |FrameType |1341|DEBUG: AU_DELIM NALU Unit Type
HLS| 04/14/16 17:56:58:933 |mpm_h264.cpp |FrameType |1341|DEBUG: SEI NALU Unit Type
HLS| 04/14/16 17:56:58:933 |mpm_h264.cpp |FrameType |1341|DEBUG: NON_IDR_PICTURE NALU Unit Type
HLS| 04/14/16 17:56:58:933 |mpm_h264.cpp |FrameType |1377|DEBUG: B Frame Received
HLS| 04/14/16 17:56:58:933 |hls_tsd_module.cpp|ProcessVideoBuffer |4151|DEBUG: SAMPLE TYPE: B - FRAME PTS: 8573002542 DTS: 8573002542
HLS| 04/14/16 17:56:58:933 |hls_tsd_module.cpp|ProcessVideoBuffer |4193|DEBUG: Video send pts 8573002542
HLS| 04/14/16 17:56:58:933 |hls_tsm_module.cpp|AlternateFrameInterLeave |11770|DEBUG: Audio Record Status is 1
HLS| 04/14/16 17:56:58:934 |hls_tsd_module.cpp|ProcessVideoBuffer |4261|DEBUG: Frame video pts 8573004043
HLS| 04/14/16 17:56:58:934 |hls_tsm_module.cpp|AlternateFrameInterLeave |11770|DEBUG: Audio Record Status is 1
HLS| 04/14/16 17:56:58:934 |hls_tsd_module.cpp|SegmentStream |1597|DEBUG: Not an AV/Subtitle Packet 256
HLS| 04/14/16 17:56:58:934 |mpm_h264.cpp |FrameType |1341|DEBUG: AU_DELIM NALU Unit Type
HLS| 04/14/16 17:56:58:934 |mpm_h264.cpp |FrameType |1341|DEBUG: SEI NALU Unit Type
HLS| 04/14/16 17:56:58:934 |mpm_h264.cpp |FrameType |1341|DEBUG: NON_IDR_PICTURE NALU Unit Type
HLS| 04/14/16 17:56:58:934 |mpm_h264.cpp |FrameType |1377|DEBUG: B Frame Received
HLS| 04/14/16 17:56:58:934 |hls_tsd_module.cpp|ProcessVideoBuffer |4151|DEBUG: SAMPLE TYPE: B - FRAME PTS: 8573004043 DTS: 8573004043
import os
os.system('grep -rnw "[FOLDER_NAME]" -e "SEARCH_STRING" > OP.txt')
Python regex search
method returns match object or None (if nothing is found). Python正则表达式
search
方法返回match对象或None(如果未找到任何内容)。 To get string from match object you need to call .group()
. 要从match对象获取字符串,您需要调用
.group()
。
In [1]: import re
In [2]: re.search("a", "aaa")
Out[2]: <_sre.SRE_Match at 0x7fe3514e98b8>
In [3]: re.search("a", "aaa").group()
Out[3]: 'a'
Check out the docs and this or this SO questions. 查看文档以及此或此问题。
This is how it can be done: 这是可以做到的:
with open("debug.txt", "r") as target:
for line in target:
if "[SAMPLE TYPE]" in line:
print(line)
Note that I used the context manager (the with
statement) for opening the file. 请注意,我使用上下文管理器(
with
语句)打开文件。 Besides, no re
is needed if you simply want to find a substring in another string. 此外,如果您只是想在另一个字符串中查找子字符串,则不需要
re
。
Try: 尝试:
if search:
print(line)
The regular expression object that is being returned will evaluate to False when it can't find anything. 当它找不到任何东西时,返回的正则表达式对象将计算为False。
From the documentation : re.search returns a MatchObject instance. 从文档 :re.search返回MatchObject实例。 The documentation shows you how to access the searched for string, in your case for example with
v1.group(0)
. 该文档显示了如何访问搜索到的字符串,例如,使用
v1.group(0)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.