Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing tabular data in Python

Tags:

python

tabular

What's the best way to print tabular data in Python? Say the data is in a 2D list and I want to create a smart looking table. What I actually have is a list of dictionaries and I want to print an intersection depending on values in the dictionaries. Something like

for val1 in my_dict:
   for val2 in my_dict:
    if val1['a'] > val2['a']:
      print 'x'

but in such a way that each column is fixed width. Writter and formatter classes seem like something in the realm of possibility, but still look complex to use when compared to, say, Perl's formatter.

Are there any existing implementations or do I have to write my own?

like image 444
Makis Avatar asked Feb 25 '11 20:02

Makis


People also ask

What is tabular data in Python?

Tabular is a package of Python modules for working with tabular data. Its main object is the tabarray class, a data structure for holding and manipulating tabular data. By putting data into a tabarray object, you'll get a representation of the data that is more flexible and powerful than a native Python representation.

Which is used when data is in tabular format in Python?

DataFrame() Function to Print Data in Table Format in Python.

How do you print a beautiful table in Python?

We'll use the PrettyTable() class to define, modify, and print tables in Python. For databases with a Python library that conforms to the Python DB-API – an SQLite database, for example – you can define a cursor object then build a table using the from_db_cursor() function from prettytable .


1 Answers

Do you know about PyPi?

DataGrid and PrettyTable seem like two good alternatives I found with a brief search. You may have to assemble the data in the format you want it (with "x" for when your condition is true) before sending it to the routines provided.

like image 76
Hut8 Avatar answered Oct 08 '22 01:10

Hut8