Using PrettyTable in Python to Display Tabular Data in the Command Line
This article explains how to install the PrettyTable library, create and populate tables with rows or columns, import data from CSV, databases, or HTML, customize output formats and styles, and selectively display or sort table content for Python command‑line tools.
When building a Python command‑line tool for managing cloud resources, displaying information as a readable table greatly improves usability; the PrettyTable library provides a simple way to generate such tables with good Unicode support.
pip install prettytable installs the library, after which you can import it with from prettytable import PrettyTable and create a table by specifying column headers, e.g., table = PrettyTable(["编号", "云编号", "名称", "IP地址"]) .
Rows can be added using table.add_row([...]) , where each list must match the number of headers; this method is demonstrated with a sample script that adds several server entries and prints the table with print(table) .
Columns can be added with table.add_column("项目", ["编号", "云编号", "名称", "IP地址"]) , allowing you to build a table without predefined headers.
Data can be imported directly from a CSV file using from_csv after opening the file, e.g., fp = open("res.csv", "r") table = from_csv(fp) , which reads the CSV and prints the resulting table.
Database query results can be turned into a table with from_db_cursor ; the article shows an example using SQLite: conn = sqlite3.connect("/tmp/aliyun.db") cur = conn.cursor() cur.execute("SELECT * FROM res") table = from_db_cursor(cur) .
HTML tables can also be parsed using from_html . The function returns a list containing a PrettyTable object, so you must print the first element, e.g., print(table[0]) , to avoid the list representation error.
PrettyTable supports multiple output formats: the default print(table) renders an ASCII table, while print(table.get_html_string()) returns an HTML‑formatted table.
You can selectively output data by specifying columns ( table.get_string(fields=["编号", "IP地址"]) ), rows ( table.get_string(start=0, end=2) ), or by slicing the table ( new_table = table[0:2] ). Sorting is available with table.get_string(sortby="编号", reversesort=True) .
Styling options include built‑in styles such as MSWORD_FRIENDLY , PLAIN_COLUMNS , DEFAULT , and RANDOM . Custom styles can be set by adjusting alignment ( table.align[1] = "l" ) and border characters ( table.border = True , table.junction_char = "$" , table.horizontal_char = "+" , table.vertical_char = "%" ), then printing the table.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.