首页 > 解决方案 > Python if 语句不循环遍历 elif 和 else 语句部分

问题描述

我正在尝试打开一个 XML 文件并对其进行解析,查看其标签并在每个特定标签中查找文本。如果标记中的文本与字符串匹配,我希望它删除字符串的一部分或用其他内容替换它。

但是,由于某种原因,代码似乎停留在第三个 if 语句中,并认为 end_int 始终等于 none。我不知道为什么,因为在找到变量 end_int 的值时,我打印了这些值,它从 xml 文件中获取了所有的“end_char”标记值,这就是 end_int 应该是的。但在 if 语句中,它认为 end_char 始终为 None。

mfn_pn 变量是用户输入的条形码,类似于 ATL-157-1815, DFW-184-8378., ATL-324-3243., DFW-432-2343, ATL 343 8924, DFW 342 3413, DFW- 324 3423 T&R。

XML 文件包含以下数据:

<?xml version="1.0" encoding="utf-8"?>
<metadata>
    <filter>
        <regex>ATL|LAX|DFW</regex >
        <start_char>3</start_char>
        <end_char></end_char>
        <action>remove</action>
    </filter>
    <filter>
        <regex>DFW.+\.$</regex >
        <start_char>3</start_char>
        <end_char>-1</end_char>
        <action>remove</action>
    </filter>
    <filter>
        <regex>\-</regex >
        <replacement></replacement>
        <action>substitute</action>
    </filter>
    <filter>
        <regex>\s</regex >
        <replacement></replacement>
        <action>substitute</action>
    </filter>
    <filter>
        <regex>1P</regex >
        <start_char>2</start_char>
        <end_char></end_char>
        <action>remove</action>
    </filter>
    <filter>
        <regex>T&#038;R$</regex >
        <start_char></start_char>
        <end_char>-4</end_char>
        <action>remove</action>
    </filter>
</metadata>

我正在使用的 Python 代码是:

import re
from xml.etree.ElementTree import ElementTree

# filters.xml is the file that holds the things to be filtered
tree = ElementTree()
tree.parse("filters.xml")

# Get the data in the XML file 
root = tree.getroot()

# Loop through filters
for x in root.findall('filter'):

    # Find the text inside the regex tag
    regex = x.find('regex').text
    # Find the text inside the start_char tag
    start_prim = x.find('start_char')
    
    # If the element exists assign its text to start variable
    start = start_prim.text if start_prim is not None else None
    start_int = int(start) if start is not None else None
    print('start: ', start_int)

    # Find the text inside the end_char tag
    end_prim = x.find('end_char')

    # If the element exists assign its text to end variable
    end = end_prim.text if end_prim is not None else None
    end_int = int(end) if end is not None else None
    print('end: ', end_int)

    # Find the text inside the action tag
    action = x.find('action').text

    if action == 'remove':
        if re.match(r'%s' % regex, mfn_pn, re.IGNORECASE):
            print('if statement start:', start_int)
            print('if statement end:', end_int)
            if end_int == None:
                print('if statement start_int:', start_int)
                print('if statement end_int:', end_int)
                mfn_pn = mfn_pn[start_int:]
            elif start_int == None:
                print('elif statement start_int:' ,start_int)
                print('elif statement end_int:', end_int)
                mfn_pn = mfn_pn[:end_int]
            else: 
                print('else statement start_int:', start_int)
                print('else statement end_int:', end_int)
                mfn_pn = mfn_pn[start_int:end_int]
    elif action == 'substitute':
        mfn_pn = re.sub(r'%s' % regex, '', mfn_pn)

对于 elif 和 else 语句中的 print 语句,没有任何输出,因为由于某种原因,代码认为 start_int 永远不会等于“None”,并且 else 语句的所有其他情况也不起作用。它认为 end_int == 'None' 总是正确的,我不确定它为什么会这样认为,因为在 if 语句之外打印出“end_int”会从 XML 文件中获取所有 end_char 值。

标签: pythonxmlif-statementvariablesprinting

解决方案


试试“DFW-324 3423 T&R”

mfn_pn = 'DFW-324 3423 T&R'
  • 第一个过滤器删除前三个字符
    • mfn_pn = '-324 3423 T&R'
      
  • 第二个过滤器正则表达式不匹配,因为该模式要求字符串以“DFW”开头。
    • mfn_pn = '-324 3423 T&R'
      
  • 第三个过滤器删除破折号
    • mfn_pn = '324 3423 T&R'
      
  • 第四个过滤器删除所有空格
    • mfn_pn='3243423T&R'
      
  • 第五个过滤器无法删除T&R,因为正则表达式模式' T&R$'注意到模式中的空间。
    • mfn_pn='3243423T&R'
      

您的第四个过滤器的 xml 数据是错误的 - 将其更改为

...
    <filter>
        <regex>.*T&amp;R$</regex >
        <start_char></start_char>
        <end_char>-4</end_char>
        <action>remove</action>
    </filter>

或将其更改为

...
    <filter>
        <regex>T&amp;R$</regex >
        <start_char></start_char>
        <end_char>-4</end_char>
        <action>substitute</action>
    </filter>

如果您希望第二个过滤器在最后删除单个句点,请将其更改为

...
    <filter>
        <regex>[.]$</regex >
        <start_char>3</start_char>
        <end_char>-1</end_char>
        <action>substitute</action>
    </filter>

请注意,每个过滤器标记迭代都可能使字符串发生变异,因此删除和替换的顺序很重要。


推荐阅读