[英]XML Attributes printing backwards in python
When I get a printout of the XML file using this code, it prints backward as shown below.当我使用此代码打印 XML 文件时,它会向后打印,如下所示。
import os
import xml.etree.cElementTree as ET
import pprint
data_directory = os.environ.get('')
def taxRules():
fileName = '/taxRules.xml'
tree = ET.parse(data_directory + fileName)
root = tree.getroot()
data_list = []
for child in root.iter("taxRule"):
data = child.attrib.copy()
# data['status'] = (data['status'])
# data['rate'] = float(data['rate'])
# data['range1'] = float(data['range1'])
data_list.append(data)
return data_list
if __name__ == '__main__':
results = taxRules()
pprint.pprint(results)
this is the printout but its suppose to print out status, rate, range1, range2这是打印输出,但它应该打印出状态、速率、范围 1、范围 2
Any idea how I can get it to print out exactly how it shown in the XML file?知道如何让它准确打印出它在 XML 文件中的显示方式吗?
I'm using python 3.8我正在使用 python 3.8
[{'range1': '0', 'range2': '9950', 'rate': '0.1', 'status': 's'},
{'range1': '9950', 'range2': '40525', 'rate': '0.12', 'status': 's'},
{'range1': '40525', 'range2': '86375', 'rate': '0.22', 'status': 's'},
{'range1': '86375', 'range2': '164925', 'rate': '0.24', 'status': 's'},
{'range1': '164925', 'range2': '209425', 'rate': '0.32', 'status': 's'},
{'range1': '209425', 'range2': '523600', 'rate': '0.35', 'status': 's'},
{'range1': '523600', 'range2': 'max', 'rate': '0.37', 'status': 's'},
{'range1': '0', 'range2': '19900', 'rate': '0.1', 'status': 'mfj'},
{'range1': '19900', 'range2': '81050', 'rate': '0.12', 'status': 'mfj'},
{'range1': '81050', 'range2': '172750', 'rate': '0.22', 'status': 'mfj'},
{'range1': '172750', 'range2': '329850', 'rate': '0.24', 'status': 'mfj'},
{'range1': '329850', 'range2': '418850', 'rate': '0.32', 'status': 'mfj'},
{'range1': '418850', 'range2': '628300', 'rate': '0.35', 'status': 'mfj'},
{'range1': '628300', 'range2': 'max', 'rate': '0.37', 'status': 'mfj'},
{'range1': '0', 'range2': '14200', 'rate': '0.1', 'status': 'hh'},
{'range1': '14200', 'range2': '54200', 'rate': '0.12', 'status': 'hh'},
{'range1': '54200', 'range2': '86350', 'rate': '0.22', 'status': 'hh'},
{'range1': '86350', 'range2': '164900', 'rate': '0.24', 'status': 'hh'},
{'range1': '164900', 'range2': '209400', 'rate': '0.32', 'status': 'hh'},
{'range1': '209400', 'range2': '523600', 'rate': '0.35', 'status': 'hh'},
{'range1': '523600', 'range2': 'max', 'rate': '0.37', 'status': 'hh'}]
It is a dictionary, so the order is not really important since you will access it by its key.它是一本字典,所以顺序并不重要,因为您将通过它的键访问它。 But if you still want to print it in order, you can re-arrange it using this code.
但是如果你仍然想按顺序打印它,你可以使用这段代码重新排列它。
reorder_dict = [{x: d[x] for x in ["status", "range1", "range2", "range3", "rate"]} for d in data_list]
Extending this idea to your taxRules()
method, you might do:将这个想法扩展到您的
taxRules()
方法,您可以这样做:
def taxRules():
fileName = '/taxRules.xml'
tree = ET.parse(data_directory + fileName)
root = tree.getroot()
data_list = [
{
key: child.attrib[key]
for key
in ["status", "rate", "range1", "range2"]
}
for child
in root.iter("taxRule")
]
return data_list
The reason why it was printing the XML file unordered was because I was using pprint.pprint instead I used this to get the print I wanted in list view....它无序打印 XML 文件的原因是因为我使用的是 pprint.pprint 而不是我用它来在列表视图中获取我想要的打印....
if __name__ == '__main__':
values = taxRules()
for v in values:
print(v)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.