[英]How to Exit/Break Python Automation Script in If Statement (Not inside a loop)
[英]Python loop if statement exit
我想在 pdf 文件的第一页中找到 2 个元素。 以 P 开头的元素和以 N 开头的元素。即使找到了 1 个元素,仍然需要继续搜索另一个元素。
如果找到P,继续搜索N 如果找到N,继续搜索P 当找到P和N时,停止搜索
我的循环有问题,只有在没有 P 的情况下才能找到 N。我知道 if 语句存在问题,但我似乎无法纠正它。
if text is not None:
p_new = "NoPFound"
n_new = "NoNFound"
for line in text.split('\n'):
#get p
if line.startswith('P '):
pieces = line.split()
p_old = pieces[1]
p_new = (p_old[7:11]) + (p_old[0:6])
break
#get n
if line.startswith('N '):
pieces = line.split()
n_old = pieces[1]
n_new = (n_old[0:15]) + "/" + (n_old[18:20])
break
list_parsed_items.append([p_new, n_new])
使用标志。 您有一台小型 state 机器。
if text is not None:
p_new = None
n_new = None
for line in text.splitlines():
#get p
if not p_new and line.startswith('P '):
pieces = line.split()
p_old = pieces[1]
p_new = (p_old[7:11]) + (p_old[0:6])
#get n
if not n_new and line.startswith('N '):
pieces = line.split()
n_old = pieces[1]
n_new = (n_old[0:15]) + "/" + (n_old[18:20])
if p_new and n_new:
break
list_parsed_items.append([p_new, n_new])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.