python打印字典全部信息_在python列表中打印字典值

I am trying to print out just the dict values inside a list in python.

car_object = {}

cursor = self._db.execute('SELECT IDENT, MAKE, MODEL, DISPLACEMENT, POWER, LUXURY FROM CARS')

for row in cursor:

objectname = 'Car_Object_'+str(row['IDENT'])

# print (objectname)

car_object[objectname] = Cars(ident = row['IDENT'], make = row['MAKE'], model = row['MODEL'], disp = row['DISPLACEMENT'], power = row['POWER'], luxury = row['LUXURY'])

print(objectname, car_object[objectname])

#print(row['IDENT'], row['MAKE'], row['MODEL'], row['DISPLACEMENT'], row['POWER'], row['LUXURY'])

yield dict(row)

So it is printing:

Car_Object_meA160 {'power': 55, 'ident': 'meA160', 'model': 'A160 CDI', 'disp': 1.7, 'make': 'mercedes', 'luxury': 'N'}

Car_Object_meA190 {'power': 92, 'ident': 'meA190', 'model': 'A190', 'disp': 1.7, 'make': 'mercedes', 'luxury': 'Y'}

Car_Object_meA210 {'power': 103, 'ident': 'meA210', 'model': 'A210 EVO', 'disp': 1.7, 'make': 'mercedes', 'luxury': 'Y'}

...and so on

I want to be able to print it like so:

IDENT MAKE MODEL DISP POWER LUX

Car_Object_meA160 meA160 mercedes A160 CDI 1.7 55 N

Car_Object_meA190 meA190 mercedes A190 1.7 92 Y

Car_Object_meA210 meA210 mercedes A210 EVO 1.7 103 Y

So i want to be able to print just the values....with the headers ordered a certain way. Is it possible to do this? Thanks.

解决方案

The tricky part is aligning the several table entries and the table headers. For this, we first have to find out how long the longest entry in each column is. pad then can be used to add a number of padding spaces to the given string.

fields = ["ident", "make", "model", "disp", "power", "luxury"]

max_len = {"name": max(map(len, car_objects)) + 1}

for f in fields:

max_len[f] = max(map(len, [f] + [str(car[f]) for car in car_objects.values()]))

pad = lambda s, f: str(s).ljust(max_len[f])

Now, we can print the headers and the several entries in car_objects using the pad function defined above.

print pad("", "name") + " ".join(pad(f.upper(), f) for f in fields)

for name, car in car_objects.items():

print pad(name, "name") + " ".join(pad(car[f], f) for f in fields)

This should work, assuming that the elements of car_objects are Python dictionaries. If not, try to replace car[f] with getattr(c, f) in the above code.

Update: Of course, perfectly aligning the columns of the table only works if you know all the rows before actually printing them. If you have to print them while still reading entries from the database, you have to 'guess' by how many spaces to pad the strings so they are nicely aligned in a table. This makes everything much simpler. Just put this line before your for loop for printing the table headers:

print (" " * 20) + " ".join(f.upper().ljust(10) for f in fields)

And this line inside your loop, before the yield:

print name.ljust(20) + " ".join(str(getattr(car, f)).ljust(10) for f in fields)

str.ljust(n) is a standard string function that return the string aligned to the left within a total width of n characters. There are similar functions for aligning right and center alignment: rjust and center. And since your cars seem to be instances of some class, you can use the builtin function getattr(, ) to retrieve the individual attributes of the cars (similar to your getVariable method).

For more on string formatting, take a look a this Python documentation page.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值