Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中使输出看起来整洁_Python_Python 2.7_Python 3.x - Fatal编程技术网

在python中使输出看起来整洁

在python中使输出看起来整洁,python,python-2.7,python-3.x,Python,Python 2.7,Python 3.x,我对python真的很陌生。我只是想知道,如何使输出看起来漂亮和干净?因为搜索和排序函数的输出如下所示 "[{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'Sup

我对python真的很陌生。我只是想知道,如何使输出看起来漂亮和干净?因为搜索和排序函数的输出如下所示

"[{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'}]
我有点希望我的输出像这样

"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”
有人能帮我吗?我已经花了一个小时的时间试图解决这个问题,这真的让我感到压力,任何帮助都将不胜感激。 这是我的密码

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
我试图替换
foundList.append(workutobject[“设备”][counter])
以打印
workutobject[“设备”][counter][“名称”]
,但它打乱了我的排序功能


谢谢

简单的答案是调查。如果您查看一下,您可以了解如何根据需要在中格式化不同的数据类型

一个较长的答案,基本上是让我们为您编写代码,但首先:

for equip in sortedTypeList:
    print '{0} {1} {2}'.format(equip['name'],equip['price'],equip['type'])

简单的答案是调查。如果您查看一下,您可以了解如何根据需要在中格式化不同的数据类型

一个较长的答案,基本上是让我们为您编写代码,但首先:

for equip in sortedTypeList:
    print '{0} {1} {2}'.format(equip['name'],equip['price'],equip['type'])

顺便说一句,谢谢你,先生,但是我应该把上面的代码放在哪里呢?进入排序功能还是搜索功能?我还是很困惑though@user3059137你可以把代码放在任何你想打印输出的地方。目前您的代码还没有打印出来,所以您需要找出该位。tyvm!耶,我知道了@用户3059137如果这个答案有帮助,你可以通过点击这个答案旁边的绿色勾号向未来的用户指出。顺便说一句,谢谢你,先生,但是我应该把上面的代码放在哪里?进入排序功能还是搜索功能?我还是很困惑though@user3059137你可以把代码放在任何你想打印输出的地方。目前您的代码还没有打印出来,所以您需要找出该位。tyvm!耶,我知道了@用户3059137如果此答案有帮助,您可以通过单击此答案旁边的绿色勾号向未来用户指出。