When I get a printout of the XML file using this code, it prints backward as shown below.
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
Any idea how I can get it to print out exactly how it shown in the XML file?
I'm using 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'}]
CodePudding user response:
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:
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
CodePudding user response:
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 ....
if __name__ == '__main__':
values = taxRules()
for v in values:
print(v)