为什么if和elif语句都在python代码中执行?

2024-06-17 12:32:12 发布

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

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

然而,出于某种原因,我的“if语句”似乎不起作用。我希望它只在变量“action”等于“remove”时做一些事情,并且只在变量“action”等于“substitute”时做一些其他事情“等于'substitute',if语句与elif语句中的内容一起执行。此外,第二个if语句中的if、elif和else语句似乎也不起作用。即使end_int不等于none,if语句中的内容也会发生,但在“start_int==none”和其他情况下,不会继续执行elif和else语句

mfn_pn变量是用户输入的条形码,类似于ATL-157-1815、DFW-184-8378、ATL-324-3243、DFW-432-2343

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> T&amp;R$</regex >
        <start_char></start_char>
        <end_char>-4</end_char>
        <action>remove</action>
    </filter>
</metadata>

我使用的Python代码是:

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

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

    # If the element exists assign its text 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

    # 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):
            if end_int == None:
                mfn_pn = mfn_pn[start_int:]
            elif start_int == None:
                mfn_pn = mfn_pn[:end_int]
            else: 
                mfn_pn = mfn_pn[start_int:end_int]
                
    elif action == 'substitute':
        mfn_pn = re.sub(r'%s' % regex, '', mfn_pn)

输出:

如果最惠国pn=1PDFW 356-5789,我得到FW3565789。它删除前3个字符,即使它应该查看xml文件,并且当regex等于1P时,只删除前两个字符,因为start_char等于2。所以mfn_pn=regex[start_int:]应该是mfn_pn=regex[2:],但出于某种原因,它仍然认为start_int是3

如果最惠国待遇pn=DFW356-5789,我得到3565789。它正在删除前三个字符,即使正则表达式与任何应该删除的字符都不匹配——它执行if语句,即使它应该跳到elif语句

它似乎只获取第一个“filter”标记中的内容,并将regex设置为仅等于第一个regex标记中的内容,start_int设置为仅等于第一个start_int中的内容,end_char设置为仅等于第一个end_int中的内容。在if语句中,它不会将regex设置为剩余过滤器标记中的内容


1条回答
网友
1楼 · 发布于 2024-06-17 12:32:12

根据您希望从1PDFW 356-5789获得的输出,它将生成3565789。如果可以更改正则表达式,我对filters.xml和python代码有如下建议

XML文件包含以下数据:

<?xml version="1.0" encoding="utf-8"?>
<metadata>
    <filter>
        <regex>ATL|LAX|DFW</regex >
        <start_char>2</start_char>
        <end_char></end_char>
        <action>remove</action>
    </filter>
    <filter>
        <regex>DFW</regex >
        <start_char>3</start_char>
        <end_char></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> T&amp;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("filter.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

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

    # If the element exists assign its text 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

    # Find the text inside the action tag
    action = x.find('action').text
    if action == 'remove':
        if re.search(r'%s\b' % regex,mfn_pn):
            mfn_pn = mfn_pn[start_int:end_int]

    elif action == 'substitute':
        mfn_pn = re.sub(r'%s' % regex, '', mfn_pn)

相关问题 更多 >