【发布时间】:2020-11-06 19:50:13
【问题描述】:
我正在解析两个结构相同但数据不同的 xml 文件。获得数字数组后,我需要将“file_1”中的每个数字与“file_2”中的类似数字进行比较,如果它大于或等于“file_2”中的数字,则发出警报。问题是,我的代码比较了整个数组,如果“file_2”中至少有一个数字大于“file_1”中的数字,我不会收到警报。
import xml.dom.minidom
import urllib.request
def main():
# using parse() for downloading and parsing XML file
urllib.request.urlretrieve('URL_to_file_1', 'file_1.xml')
urllib.request.urlretrieve('URL_to_file_2', 'file_2.xml')
doc_1 = xml.dom.minidom.parse("file_1")
doc_2 = xml.dom.minidom.parse("file_2")
availableMethods = ['method1', 'method2']
availableData = ['data1', 'data2']
# getting list of XML tags from document
items_1 = doc_1.getElementsByTagName("item")
items_2 = doc_2.getElementsByTagName("item")
for item in items_1:
method_1 = item.getElementsByTagName("from")[0]
to_1 = item.getElementsByTagName("to")[0]
out_1 = item.getElementsByTagName("out")[0]
if method_1.firstChild.data in availableMethods and to_1.firstChild.data in availableData:
numbers_1 = float(out_1.firstChild.data)
print("method_1:%s, to_1:%s, out_1:%s" % (method_1.firstChild.data, to_1.firstChild.data, out_1.firstChild.data))
for item in items_2:
method_2 = item.getElementsByTagName("from")[0]
to_2 = item.getElementsByTagName("to")[0]
out_2 = item.getElementsByTagName("out")[0]
if method_2.firstChild.data in availableMethods and to_2.firstChild.data in availableData:
numbers_2 = float(out_2.firstChild.data)
print("method_2:%s, to_2:%s, out_2:%s" % (method_2.firstChild.data, to_2.firstChild.data, out_2.firstChild.data))
if numbers_2 <= numbers_2:
print("OK")
else:
print("BAD")
if __name__ == "__main__":
main()
file_1.xml
<rates>
<item>
<from>method1</from>
<to>data1</to>
<in>1</in>
<tofee>0.0006</tofee>
<out>0.00006084091504736</out>
<amount>39.508888709782</amount>
<minamount>105</minamount>
<maxamount>10000</maxamount>
</item>
<item>
<from>method2</from>
<to>data1</to>
<in>1</in>
<tofee>0.0006</tofee>
<out>0.00007190617745180</out>
<amount>39.508888709782</amount>
<minamount>90</minamount>
<maxamount>10000 EUR</maxamount>
</item>
<item>
<from>method1</from>
<to>data2</to>
<in>1</in>
<tofee>0.0006</tofee>
<out>0.00225030292539380</out>
<amount>39.508888709782</amount>
<minamount>105</minamount>
<maxamount>10000</maxamount>
</item>
<item>
<from>method2</from>
<to>data2</to>
<in>1</in>
<tofee>0.0006</tofee>
<out>0.00266214200040956</out>
<amount>39.508888709782</amount>
<minamount>90</minamount>
<maxamount>10000</maxamount>
</item>
</rates>
file_2.xml
<rates>
<item>
<from>method1</from>
<to>data1</to>
<in>1</in>
<tofee>0.0006</tofee>
<out>0.00076084091504736</out>
<amount>39.508888709782</amount>
<minamount>105</minamount>
<maxamount>10000</maxamount>
</item>
<item>
<from>method2</from>
<to>data1</to>
<in>1</in>
<tofee>0.0006</tofee>
<out>0.00077190617745180</out>
<amount>39.508888709782</amount>
<minamount>90</minamount>
<maxamount>10000</maxamount>
</item>
<item>
<from>method1</from>
<to>data2</to>
<in>1</in>
<tofee>0.0006</tofee>
<out>0.07225030292539380</out>
<amount>39.508888709782</amount>
<minamount>105</minamount>
<maxamount>10000</maxamount>
</item>
<item>
<from>method2</from>
<to>data2</to>
<in>1</in>
<tofee>0.0006</tofee>
<out>0.07266214200040956</out>
<amount>39.508888709782</amount>
<minamount>90</minamount>
<maxamount>10000</maxamount>
</item>
</rates>
我需要将“file_1”中方法+数据对中的每个“out”值与“file_2”中类似的“out”值进行比较,并在“file_1”中的“out”大于或等于时发出警报“file_2”中的“out”。
【问题讨论】:
-
分享 xml 文件并解释预期输出是什么
-
添加 xml 文件和预期输出
标签: python python-3.x