为什么判断字段是否在文本中时,if和else不能同时用呢?只用if,可以if下的print,但是用了else,就全部输出else下的print!
def check(str):
with open(r'C:\Users\yida\Desktop\feedslogcat\1.txt') as f_r:
for line in f_r.readlines():
if str in line:
return print(line)
else:
return print(str,':','字段不存在!')
r1 = str('Boot')
r2 = str('Begin construct Feeds')
check(r1)
check(r2)
或者不用return,也是不行的
def check(str):
with open(r'C:\Users\yida\Desktop\feedslogcat\1.txt') as f_r:
for line in f_r.readlines():
if str in line:
print(line)
break
else:
print(str,':','字段不存在!')
break