我有这样的输入
line 1: [DEBUG]...
line 2: [DEBUG]...
line 2: [DEBUG]...
由此我想只打印第一个匹配的字符串
第一个匹配line 1: [DEBUG]
并停止遍历。
我尝试过以下代码:
for num1,line1 in enumerate(reversed(newline),curline):
ustr1="[DEBUG]"
if ustr1 in line1:
firstnum=num1
任何人都可以帮助我吗?
答案 0 :(得分:0)
你的问题格式不是很好,我无法真正看到你的对象看起来像是什么......我会假设它是这样的:
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
希望我明白这一点;)