Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 在python中,如何仅打印日志输出中的第一个匹配行?_Python 2.7 - Fatal编程技术网

Python 2.7 在python中,如何仅打印日志输出中的第一个匹配行?

Python 2.7 在python中,如何仅打印日志输出中的第一个匹配行?,python-2.7,Python 2.7,我有这样的输入 line 1: [DEBUG]... line 2: [DEBUG]... line 2: [DEBUG]... 从这里,我只想打印第一个匹配的字符串 第一个匹配行1:[DEBUG]并停止遍历 我尝试了以下代码: for num1,line1 in enumerate(reversed(newline),curline): ustr1="[DEBUG]" if ustr1 in line1: firstnum=num1 有人能帮我吗 你的

我有这样的输入

line 1: [DEBUG]...
line 2: [DEBUG]... 
line 2: [DEBUG]... 
从这里,我只想打印第一个匹配的字符串 第一个匹配行1:[DEBUG]并停止遍历

我尝试了以下代码:

for num1,line1 in enumerate(reversed(newline),curline):
    ustr1="[DEBUG]" 
    if ustr1 in line1:
        firstnum=num1

有人能帮我吗

你的问题格式不太好,我真的看不出你的物体是什么样子。。。。我想是这样的:

input="1: [DEBUG]....\n2: [DEBUG]...\n3:...."
# now you could do e.g.:
print(input.split("\n")[0].strip("\r"))
# which would be the first line. as you are searching a line containing a certain string "ustr1", you could do:
for line in input.split("\n"):
    if ustr1 in line:
        print(line)
        break #as you dont want more than one line
#furthermore, if you need the index of the line, do:
for i,line in enumerate(input.split("\n")):
     pass #i = index, line = line
希望我理解正确