PrettyTable

Project Address: https://github.com/mapio/prettytable-mirror

PrettyTable

A simple Python library for easily displaying tabular data in a visually appealing ASCII table format.

一个第三方库,需要单独安装

TUTORIAL ON HOW TO USE THE PRETTYTABLE 0.6+ API

This tutorial is distributed with PrettyTable and is meant to serve as a "quick start" guide for the lazy or impatient. It is not an exhaustive description of the whole API, and it is not guaranteed to be 100% up to date. For more complete and update documentation, check the PrettyTable wiki at http://code.google.com/p/prettytable/w/list

Getting your data into (and out of) the table

Let's suppose you have a shiny new PrettyTable:

from prettytable import PrettyTable
x = PrettyTable()

and you want to put some data into it. You have a few options.

Row by row

You can add data one row at a time. To do this you can set the field names first using the field_names attribute, and then add the rows one at a time using the add_row method:

x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
x.add_row(["Adelaide",1295, 1158259, 600.5])
x.add_row(["Brisbane",5905, 1857594, 1146.4])
x.add_row(["Darwin", 112, 120900, 1714.7])
x.add_row(["Hobart", 1357, 205556, 619.5])
x.add_row(["Sydney", 2058, 4336374, 1214.8])
x.add_row(["Melbourne", 1566, 3806092, 646.9])
x.add_row(["Perth", 5386, 1554769, 869.4])

 

Column by column

You can add data one column at a time as well. To do this you use the add_column method, which takes two arguments - a string which is the name for the field the column you are adding corresponds to, and a list or tuple which contains the column data"

x.add_column("City name",["Adelaide","Brisbane","Darwin","Hobart","Sydney","Melbourne","Perth"])
x.add_column("Area", [1295, 5905, 112, 1357, 2058, 1566, 5386])
x.add_column("Population", [1158259, 1857594, 120900, 205556, 4336374, 3806092,1554769])
x.add_column("Annual Rainfall",[600.5, 1146.4, 1714.7, 619.5, 1214.8, 646.9,869.4])

 

Mixing and matching

If you really want to, you can even mix and match add_row and add_column and build some of your table in one way and some of it in the other. There's a unit test which makes sure that doing things this way will always work out nicely as if you'd done it using just one of the two approaches. Tables built this way are kind of confusing for other people to read, though, so don't do this unless you have a good reason.

Importing data from a CSV file

If you have your table data in a comma separated values file (.csv), you can read this data into a PrettyTable like this:

from prettytable import from_csv
fp = open("myfile.csv", "r")
mytable = from_csv(fp)
fp.close()

 

Importing data from a database cursor

If you have your table data in a database which you can access using a library which confirms to the Python DB-API (e.g. an SQLite database accessible using the sqlite module), then you can build a PrettyTable using a cursor object, like this:

import sqlite3
from prettytable import from_cursor

connection = sqlite3.connect("mydb.db")
cursor = connection.cursor()
cursor.execute("SELECT field1, field2, field3 FROM my_table")
mytable = from_cursor(cursor)

 

Getting data out

There are three ways to get data out of a PrettyTable, in increasing order of completeness:

  • The del_row method takes an integer index of a single row to delete.
  • The clear_rows method takes no arguments and deletes all the rows in the table - but keeps the field names as they were so you that you can repopulate it with the same kind of data.
  • The clear method takes no arguments and deletes all rows and all field names. It's not quite the same as creating a fresh table instance, though - style related settings, discussed later, are maintained.

Displaying your table in ASCII form

PrettyTable's main goal is to let you print tables in an attractive ASCII form, like this:

+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
| Adelaide  | 1295 |  1158259   |      600.5      |
| Brisbane  | 5905 |  1857594   |      1146.4     |
| Darwin    | 112  |   120900   |      1714.7     |
| Hobart    | 1357 |   205556   |      619.5      |
| Melbourne | 1566 |  3806092   |      646.9      |
| Perth     | 5386 |  1554769   |      869.4      |
| Sydney    | 2058 |  4336374   |      1214.8     |
+-----------+------+------------+-----------------+

You can print tables like this to stdout or get string representations of them.

posted @ 2017-02-08 01:58  Vincen_shen  阅读(389)  评论(0)    收藏  举报