Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在另一个If语句中包含比较运算符的If语句未运行_Python - Fatal编程技术网

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

Python 在另一个If语句中包含比较运算符的If语句未运行,python,Python,我是Python新手。我试图建立一个规则,如果满足两个条件,程序将生成一个变量,但由于某种原因,如果该规则的语句未运行,则程序将生成一个变量 让我解释一下我所做工作的背景:我有以下列表和一个函数,该函数返回与任意两个给定列表、字典、字符串等匹配的任何元素: poa_term_variations = ['power of attorney', 'poa'] poa_corporate_identifier = ["to attend any partners’ meeting", 'to rep

我是Python新手。我试图建立一个规则,如果满足两个条件,程序将生成一个变量,但由于某种原因,如果该规则的语句未运行,则程序将生成一个变量

让我解释一下我所做工作的背景:我有以下列表和一个函数,该函数返回与任意两个给定列表、字典、字符串等匹配的任何元素:

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\u corporate\u identifier
列表在
text
变量中至少包含一个术语匹配,并且(ii)
poa\u cnpj\u identifier
text
变量中没有任何术语匹配。这是我得到的错误:

>>> work_summary = ['Work description: ' + 'Drafting PoA for the purposes of ' + str(document_nature) + '.']
>>> NameError: name 'document_nature' is not defined 
请注意,我用来测试代码的其他示例文档以及与第二条
if
语句和第二条
elif
语句中的条件相匹配的文档运行正常


已经尝试了其他比较运算符(
!=
,前提是该
elif
语句中的表达式与第一个
if
语句中的表达式完全相同(
len(术语跟踪程序(文本,poa\U公司标识符))>0
),或者只运行
if
语句,或者任何其他检查不同条件的
elif
/
else


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

elif
表示
如果
,如果第一个条件为真,它将不会运行。在您的情况下,
如果
elif
条件相等,则永远不会达到第二个条件


另外,您正在运行
以获取文本中的字符串:
循环,并且只在内部使用
文本
。您可能应该使用
字符串

哦,好的,我明白了,谢谢@ForceBru!您对我如何解决这个问题有什么建议吗?@gcqhk19,我想您是想移动
if len(术语跟踪程序(text,poa\u cnpj\u标识符))==0:
第一个
if
语句中的内容:
if len(术语跟踪程序(文本,poa\U公司标识符))>0:
,以及检查是否
len(术语跟踪程序(文本,poa\U公司标识符))的语句)>0
是正确的。谢谢@Heavy!我对这一点还不熟悉,所以我一直在与Python搏斗。你对如何解决这个问题有什么想法吗?