[英]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.