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(<object>, <attribute name>)
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.