python - 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&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 值。
解决方案
试试“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&R$</regex >
<start_char></start_char>
<end_char>-4</end_char>
<action>remove</action>
</filter>
或将其更改为
...
<filter>
<regex>T&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>
请注意,每个过滤器标记迭代都可能使字符串发生变异,因此删除和替换的顺序很重要。
推荐阅读
- angularjs - Overriding the items in the json while using ng repeat
- javascript - 如何将来自 REST API 响应的数据存储在变量中?
- php - PHPMailer error "Message body empty" with Gmail
- c++ - 为什么在进行条件变量通知之前我们需要一个空的 std::lock_guard?
- sql-server - 在 SQL Server 中使用动态查询进行透视
- python - how to set environment variable from .env file in python script?
- c# - How to initialize login functionality in Site.Master form to pull Login ID from SQL after redirect?
- azure - Get-AzureRmSnapshot 引发错误 Get-AzureRmSnapshot:方法“get_SerializationSettings”
- android - 是否可以有相同的 Android 应用程序有或没有 facebook 登录?
- javascript - 如何修复 DataTables autoWidth 不起作用