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
希望我理解正确