In the Python programming language, we often need to output data at the terminal and sometimes we need to present data in a table form. Python provides some libraries to help us implement the function of outputting tables on the terminal, which can make the data more intuitive and easy to read. This article will explain how to use these libraries in Python to output tables and combine code examples to help readers better understand.
Use the tabulate library to output table
In Python, a commonly used library to output tables is the tabulate library. This library allows us to output beautiful tables on the terminal and supports a variety of different formatted outputs. First, we need to install the tabulate library:
pip install tabulate
Next, let's look at a simple example of outputting a table using the tabulate library:
from tabulate import tabulate data = [ ["Alice", 24], ["Bob", 30], ["Charlie", 28] ] headers = ["Name", "Age"] table = tabulate(data, headers=headers, tablefmt="grid") print(table)
Run the above code and we will output the following table in the terminal:
+---------+-----+
| Name | Age |
+---------+-----+
| Alice | 24 |
| Bob | 30 |
| Charlie | 28 |
+---------+-----+
As you can see, using the tabulate library, you can output a beautiful table simply and quickly, making the data easier to understand.
Use prettytable library to output tables
In addition to the tabulate library, there is another commonly used library that is the prettytable library. This library can also be used to output tables in the terminal and provides more customization options. Similarly, we need to install the prettytable library first:
pip install prettytable
Next, let's take a look at an example of outputting tables using the prettytable library:
from prettytable import PrettyTable table = PrettyTable() table.field_names = ["Name", "Age"] table.add_row(["Alice", 24]) table.add_row(["Bob", 30]) table.add_row(["Charlie", 28]) print(table)
Run the above code and we will output a table similar to the following in the terminal:
+---------+-----+
| Name | Age |
+---------+-----+
| Alice | 24 |
| Bob | 30 |
| Charlie | 28 |
+---------+-----+
The prettytable library also provides a simple and easy-to-use interface to output tables, and you can choose the right library to use according to your needs.
Pie chart example
In addition to outputting tables, sometimes we also need to output some charts in the terminal, such as pie charts. In Python, we can use the matplotlib library to implement drawing pie charts and can output them to the terminal.
Here is a simple example of drawing a pie chart and outputting it to the terminal:
import as plt sizes = [25, 35, 20, 20] labels = ['A', 'B', 'C', 'D'] (sizes, labels=labels, autopct='%1.1f%%') ()
Run the above code and we will output a simple pie chart in the terminal to show the proportion of each part.
Relationship diagram example
In addition, sometimes we also need to output a relationship diagram in the terminal to show the relationship between the data. In Python, we can use the graphviz library to draw relationship graphs and output them to the terminal.
Here is an example of a simple diagram and outputting it to the terminal:
from graphviz import Digraph dot = Digraph() ('A', 'Alice') ('B', 'Bob') ('C', 'Charlie') (['AB', 'BC']) print()
Run the above code and we will output a simple relationship diagram in the terminal to show the relationship between the data. The graphviz library provides powerful functions to draw various types of graphics, and can draw relationship diagrams of different styles according to your needs.
Pandas output table using Python's third-party library
Pandas is a data processing library in Python that can be used to read, process and output tabular data. Installation method:
pip install pandas
How to use it is as follows:
import pandas as pd data = [["Zhang San", "20", "male"], ["Li Si", "22", "female"]] df = (data, columns=["Name", "age", "gender"]) print(df)
The output result is as follows:
Name Age Gender
0 Zhang San 20 Male
1 Li Si 22 Female
Among them, ``Create a table object, `columns` sets the column name of the table, and data is passed in through the list.
Using Texttable Library
To useTexttable
The library prints the above table, you can follow the following example code:
from texttable import Texttable headers = ['fruit', 'price', 'nation'] fruits = [ ['apple', 4, 'China'], ['orange', 5, 'China'], ['pear', 6, 'China'], ['Pineapple', 7, 'China'], ] table = Texttable() table.set_cols_align(['l', 'r', 'l']) (headers) table.add_rows(fruits) print(())
Run the above code and the following table will be output:
+--------+-------+--------+
| Fruits | Prices | Country |
+========+=======+========+
| Apple | 4 | China |
+--------+-------+--------+
| Orange | 5 | China |
+--------+-------+--------+
| Pear | 6 | China |
+--------+-------+--------+
| Pineapple | 7 | China |
+--------+-------+--------+
In this example, we first import the Texttable class. Then, create a Texttable object and use the set_cols_align() method to set the alignment of the columns (‘l’ means left-align, ‘r’ means right-align, and ‘c’ means center-align). Next, use the header() method to set the header and add data row by row using the add_rows() method. Finally, use the draw() method to generate a string representation of the table and print it out using the print() function.
Texttable provides some other methods and options that can be used to customize the style and format of a table. You can consult the Texttable documentation for more details and customize it as needed.
This is the article about the common ways of outputting content to the terminal in Python in table form. For more related Python terminal output table content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!