'Can't get past this TypeError in python when dealing with imported XML file
I'm trying to sort the imported data from an XML, then slicing it to take the first 10 and returning it to the my_func dictionary to then print out the rows of data from the key being called by the for loop
from helper import fixNumber
import xml.etree.cElementTree as ET
tree = ET.parse("properties.xml")
root = tree.getroot()
list = []
for child in root:
data = child.attrib.copy()
data['netIncome'] = fixNumber(child.text)
data["id"] = (data["id"])
data['cost'] = fixNumber(data['cost'])
data['downPayment'] = fixNumber(data['downPayment'])
data['state'] = (data['state'])
data['percentage'] = fixNumber(data['percentage'])
list.append(data)
def top_ten(data): # sort top ten by cost in descending order
return data['cost']
list.sort(key=top_ten, reverse=True)
for x in list[:10]:
print(x)
if __name__ == '__main__':
my_func = {
1: top_ten
}
for key in my_func:
funct = my_func[key]
print(f"{key}", funct())
This is the TypeError I keep getting
Traceback (most recent call last):
File "/Users/school/VisualStudio/01.LESSON/properties.py", line 40, in <module>
print(f"{key}", funct())
TypeError: top_ten() missing 1 required positional argument: 'data'
when I type data inside the parathesis of funct, it only prints one data point of an attribute ie. 1 144000.0 which is just one net income from the data
I need to print out the first 10 rows of all the attributes with all the data, sorted. hope this makes sense.
This is a sample of the properties.xml file
<properties>
<property id="H00001" cost="106000" downPayment="24380" state="NM" percentage="0.12">2925.6</property>
<property id="H00002" cost="125000" downPayment="30000" state="AZ" percentage="0.15">4500</property>
<property id="H00003" cost="119000" downPayment="24990" state="NH" percentage="0.13">3248.7</property>
<property id="H00004" cost="124000" downPayment="31000" state="MI" percentage="0.19">5890</property>
<property id="H00005" cost="143000" downPayment="34320" state="CZ" percentage="0.11">3775.2</property>
<property id="H00006" cost="139000" downPayment="30580" state="VI" percentage="0.12">3669.6</property>
<property id="H00007" cost="132000" downPayment="26400" state="ND" percentage="0.19">5016</property>
<property id="H00008" cost="134000" downPayment="26800" state="CZ" percentage="0.17">4556</property>
<property id="H00009" cost="143000" downPayment="34320" state="PA" percentage="0.14">4804.8</property>
<property id="H00010" cost="123000" downPayment="25830" state="IN" percentage="0.2">5166</property>
</properties>
the output should look like this but sorted by cost
1 {'id': 'H00012', 'cost': 150000.0, 'downPayment': 36000.0, 'state': 'OR', 'percentage': 0.11, 'netIncome': 3960.0}
{'id': 'H00061', 'cost': 150000.0, 'downPayment': 34500.0, 'state': 'MD', 'percentage': 0.1, 'netIncome': 3450.0}
{'id': 'H00072', 'cost': 150000.0, 'downPayment': 31500.0, 'state': 'MI', 'percentage': 0.15, 'netIncome': 4725.0}
2 {'id': 'H00012', 'cost': 150000.0, 'downPayment': 36000.0, 'state': 'OR', 'percentage': 0.11, 'netIncome': 3960.0}
{'id': 'H00061', 'cost': 150000.0, 'downPayment': 34500.0, 'state': 'MD', 'percentage': 0.1, 'netIncome': 3450.0}
{'id': 'H00072', 'cost': 150000.0, 'downPayment': 31500.0, 'state': 'MI', 'percentage': 0.15, 'netIncome': 4725.0}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
