简体   繁体   English

如何从文件中打印出与大文本文件中的输入字符串匹配的行?

[英]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.

相关问题 如果我的输入存在于替代文本文件中,如何根据文本文件中的特定行打印字符串 - How to Print a string from a specific line in a text file based off if my input exists in an alternate text file 如何从巨大的文本文件(&gt; 16GB)中提取特定行,其中每行以另一个输入文件中指定的字符串开头? - How to extract specific lines from a huge text file (>16GB) where each line starts with a string specified in another input file? 如何从文本文件中打印一行特定的文本? - How can you print a specific line of text from a text file? 如何从文本文件打印特定的行/行? - How to print a specific line/row from a text file? 如何从文件中打印特定行 - How to print a specific line from a file 如何从.INI文件中读取字符串,然后从文本文件中读取该字符串并使用Python打印整行? - How to read the string from an .INI file and then read that string from a text file and print the entire line using Python? 如何从文本文件中读取一行并打印 - How to read a line from a text file and print it 如何从HTML文件中打印一行文本 - How to print a line of text from an HTML file Python:从文本文件打印特定范围的行 - Python: Print specific range of line from text file 无法从文本文件中打印特定行 - Can't print a specific line from text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM