在另一个If语句中包含比较运算符的If语句未运行

2024-05-15 14:42:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python还不熟悉。我试图构建一个规则,如果满足两个条件,程序将生成一个变量,但是由于某些原因,这个规则的if语句没有运行

让我解释一下我正在做的事情的上下文:我有以下列表和一个函数,它返回与任何2个给定列表、字典、字符串等匹配的任何元素:

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_identifiertext变量中没有任何匹配项。这是我得到的错误:

>>> work_summary = ['Work description: ' + 'Drafting PoA for the purposes of ' + str(document_nature) + '.']
>>> NameError: name 'document_nature' is not defined 

请注意,我用于测试代码的其他示例文档以及与第二条if语句和第二条elif语句中的条件匹配的示例文档运行正常

已经尝试过其他比较运算符(!=<=is not,等等),但没有成功

如何解决这个问题


Tags: ofthetotextinforifany
2条回答

elif表示else if,如果第一个条件为真,它将不会运行。在您的例子中,ifelif条件是相等的,因此第二个条件永远不会达到

另外,您正在运行for string in text:循环,并且只在内部使用text。您可能应该改用string

如果该elif语句中的表达式与第一个if语句(len(term_tracker(text, poa_corporate_identifier)) > 0)中的表达式完全相同,则只有该if语句将运行,或者任何其他elif/else语句将检查不同的条件

这假设term_tracker在给定相同的参数时返回相同的结果,因此term_tracker(A, B) == term_tracker(A, B)对于所有AB

相关问题 更多 >