简体   繁体   English

具有比较运算符的if语句在另一个if语句中未运行

[英]If statement with comparison operators inside another if statement not running

I am new to Python. 我是Python的新手。 I am trying to build a rule where if two conditions are met the program would generate a variable, but for some reason if statements of this rule are not running. 我正在尝试建立一个规则,如果同时满足两个条件,程序将生成一个变量,但是由于某种原因, if该规则的语句未运行。

Let me explain the context of what I am doing: I have the following lists and a function that returns any elements matching any 2 given lists, dictionaries, strings, etc.: 让我解释一下我正在做的事情的上下文:我有以下列表和一个函数,该函数返回与任何两个给定列表,字典,字符串等匹配的元素:

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

For some reason, whenever I try to pass the following chunk of code, the first elif statement does not run: 由于某种原因,每当我尝试传递以下代码块时,第一个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) + '.']

I know that the first if statement runs because print('Found PoA type') runs. 我知道第一个if语句将运行,因为print('Found PoA type')将运行。 However, the first elif statement should also run as (i) poa_corporate_identifier list contains at least one term matching in text variable, and (ii) poa_cnpj_identifier does not have any term matching in text variable. 但是,第一个elif语句也应运行,因为(i) poa_corporate_identifier列表在text变量中至少包含一个术语匹配,并且(ii) poa_cnpj_identifiertext变量中没有任何术语匹配。 This is the error I get: 这是我得到的错误:

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

Note that other sample documents I am using to test my code and that match the conditions in the second if statement and the second elif statement run properly. 请注意,我正在使用其他示例文档来测试我的代码,这些示例文档与第二条if语句和第二条elif语句中的条件匹配,可以正常运行。

Already tried other comparison operators ( != , <= , is not , etc.), but no success. 已经尝试了其他比较运算符( !=<=is not等),但没有成功。

How can I solve this issue? 我该如何解决这个问题?

Provided that the expression in that elif statement is exactly the same as the one in the first if statement ( len(term_tracker(text, poa_corporate_identifier)) > 0 ), either only that if statement will run, or any other elif / else that checks for a different condition. 条件是,所述表达elif语句是完全一样的一个在所述第一if语句( len(term_tracker(text, poa_corporate_identifier)) > 0 ),要么只有if语句将运行,或者任何其他elif / else支票对于不同的条件。

This assumes that term_tracker returns the same results when given the same arguments, so that term_tracker(A, B) == term_tracker(A, B) for all A and B . 假设给定相同的参数时term_tracker返回相同的结果,因此对所有AB来说term_tracker(A, B) == term_tracker(A, B)

elif means else if and it will not run if the first condition is true. elif表示else if ,如果第一个条件为true,则它将不运行。 In you case if and elif conditions are equal, so the second condition will never be reached. ifelif条件相等,则永远不会达到第二个条件。

Also you are running for string in text: loop and only using text inside. 另外,您正在运行for string in text:循环中的for string in text:并且仅在其中使用text You probably should use string instead. 您可能应该使用string代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM