Reputation: 11
I'm really new to python. I was just wondering, how do you make the output to look nice and clean? because my output for search and sort functions looks like this
"[{u'id': 1,
u'name': u'ProPerformance',
u'price': u'$2000',
u'type': u'Treadmill'},
{u'id': 2, u'name': u'Eliptane', u'price': u'$1500', u'type': u'Elliptical'},
{u'id': 5,
u'name': u'SupremeChest',
u'price': u'$4000',
u'type': u'Chest Press'},
{u'id': 12, u'name': u'PowerCage', u'price': u'$5000', u'type': u'Squat'}]
---Sorted by Type---
[{u'id': 5,
u'name': u'SupremeChest',
u'price': u'$4000',
u'type': u'Chest Press'},
{u'id': 2, u'name': u'Eliptane', u'price': u'$1500', u'type': u'Elliptical'},
{u'id': 12, u'name': u'PowerCage', u'price': u'$5000', u'type': u'Squat'},
{u'id': 1,
u'name': u'ProPerformance',
u'price': u'$2000',
u'type': u'Treadmill'}]
I kinda want my output to look something like this
"RunPro $2000 Treadmill
Eliptane $1500 Elliptical
SupremeChest $4000 Chest Press
PowerCage $5000 Squat”
---Sorted by Type---
RunPro $2000 Chest Press
Eliptane $1500 Elliptical
SupremeChest $4000 Squat
PowerCage $5000 Treadmill”
Can someone please help me? I've been trying to figure this out for like an hour and it's really stressing me out any help would be greatly appreciated. here’s my code
def searchEquipment(self,search):
foundList = []
workoutObject =self.loadData(self.datafile)
howmanyEquipment = len(workoutObject["equipment"])
for counter in range(howmanyEquipment):
name = workoutObject["equipment"][counter]["name"].lower()
lowerCaseSearch = search.lower()
didIfindIt = name.find(lowerCaseSearch)
if didIfindIt >= 0:
foundList.append(workoutObject["equipment"][counter])
return foundList
def sortByType(self,foundEquipment):
sortedTypeList = sorted(foundEquipment, key=itemgetter("type"))
return sortedTypeList
I tried to replace foundList.append(workoutObject["equipment"][counter])
to print workoutObject["equipment"][counter]["name"]
but it messes up my sort function.
thanks
Upvotes: 0
Views: 1798
Reputation:
The short answer is to look into string formatting operations in Python. If you look there you can figure out how to format different datatypes in according to your needs.
A longer answer, would basically have us writing your code for you, but as a starter:
for equip in sortedTypeList:
print '{0} {1} {2}'.format(equip['name'],equip['price'],equip['type'])
Upvotes: 5