我是Python的新手。我正在尝试建立一条规则,如果同时满足两个条件,程序将生成一个变量,但是由于某些原因,该规则的if
语句未运行。
让我解释一下我正在做的事情的上下文:我有以下列表和一个函数,该函数返回与任何两个给定列表,字典,字符串等匹配的元素:
poa_term_variations = ['power of attorney', 'poa']
poa_corporate_identifier = ["to attend any partners’ meeting", 'to represent the grantor regarding any change or amendment to the articles of association', "to subscribe for new quotas of the company's capital"]
poa_cnpj_identifier = ['brazilian federal revenue office', 'basic cnpj entry document']
#text variable is actually information from a .txt file in my code, which I converted into a list just like below.
text = ['da#897-0095-v4',
'd#30/04/2019',
'h#2.0',
'power of attorney',
'(a) to represent the grantor in its capacity of partner of the limited
liability company hk co., with head office in xxxxx, enrolled with the
general taxpayers registry (cnpj/mf) under no. xxxxx and with the registry of
companies under no. xxxxx (hereinafter referred to as company); (b) to attend any
partners meeting of the company and vote the quotas of the grantor
as instructed by the grantor, by email sent from mr. [complete name], in relation
to any matter submitted to the appreciation of the partners, including, but not limited
to, the approval of financial statements and election of managers, officers
and/or directors; (c) to represent the grantor regarding any change or amendment
to the articles of association approved by the grantor; (d) to subscribe for new quotas of the company\'s capital approved by the grantor']
#this function verifies if a certain term inside a list is also located in another list
def term_tracker(document, term_variations):
terms = []
#If term_variations is a list
if isinstance(term_variations, list) == True:
for term in term_variations:
#If we find a term in the document, append that term to a list
if any([str(term) in i for i in document]):
terms.append(term)
#If term_variations is a string, find that string in all documents
elif isinstance(term_variations, str) == True:
if any([term_variations in i for i in document]) == True:
terms.append(term_variations)
return terms
由于某种原因,每当我尝试传递以下代码段时,第一个elif
语句都不会运行:
for string in text:
if len(term_tracker(text[0:4], poa_term_variations)) > 0:
print('Found PoA type')
document_type = term_tracker(text, poa_term_variations)
if len(term_tracker(text, poa_corporate_identifier)) > 0:
if len(term_tracker(text, poa_cnpj_identifier)) > 0:
document_nature = 'granting powers for corporate representation and representation before the Federal Revenue Office'
print('Found PoA Corporate/CNPJ type')
break
#THIS IS THE STATEMENT THAT SHOULD RUN AND IT IS NOT RUNNING
elif len(term_tracker(text, poa_corporate_identifier)) > 0:
if len(term_tracker(text, poa_cnpj_identifier)) == 0:
document_nature = 'granting powers for corporate representation'
print('Found PoA Corporate type')
break
elif len(term_tracker(text, poa_cnpj_identifier)) > 0:
print('ok1')
if len(term_tracker(text, poa_corporate_identifier)) == 0:
print('ok2')
document_nature = 'granting powers for representation before the Federal Revenue Office'
print('Found PoA CNPJ type')
work_summary = ['Work description: ' + 'Drafting PoA for the purposes of ' + str(document_nature) + '.']
我知道第一个if
语句会运行,因为print('Found PoA type')
会运行。但是,第一个elif
语句也应以(i)运行
poa_corporate_identifier
列表在text
变量中至少包含一个术语匹配,并且(ii)poa_cnpj_identifier
在text
变量中没有任何术语匹配。这是我得到的错误:
>>> work_summary = ['Work description: ' + 'Drafting PoA for the purposes of ' + str(document_nature) + '.']
>>> NameError: name 'document_nature' is not defined
请注意,我正在使用其他示例文档来测试我的代码,并且它们与第二条if
语句和第二条elif
语句中的条件匹配,并且可以正常运行。
已经尝试了其他比较运算符(!=
,<=
,is not
等),但没有成功。
我该如何解决这个问题?
答案 0 :(得分:3)
假设该elif
语句中的表达式与第一个if
语句(len(term_tracker(text, poa_corporate_identifier)) > 0
)中的表达式完全相同,或者仅运行if
语句,或检查不同条件的任何其他elif
/ else
。
这假设term_tracker
在给定相同参数时返回相同的结果,因此term_tracker(A, B) == term_tracker(A, B)
用于所有A
和B
。
答案 1 :(得分:2)
elif
的意思是else if
,如果第一个条件为true,它将不会运行。如果您的if
和elif
条件相等,那么就永远不会达到第二个条件。
此外,您正在运行for string in text:
循环,并且仅在内部使用text
。您可能应该改用string
。