Prettytable module of python

//

Prettytable module of python

//

The prettytable module of python allows us to format and print data records more clearly. Today, let's briefly look at the usage of this module.

Note: The package needs to be imported before use

from prettytable import PrettyTable

from prettytable import from_csv

If the corresponding python package does not exist, you need to pass:

pip install prettytable

To install it.

Method 1: directly add column names and data records

from prettytable import PrettyTable
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

# Enter rows and columns directly to arrange
table =PrettyTable()
table.field_names =["Name","age","gender","height"]

table.add_row(["yeyz",26,'male',182])
table.add_row(["Ren Yingying",27,'Female',172])
table.add_row(["Yang Guo",28,'male',175])

# Add a column
table.add_column("Grades",[60,70,80])
print table

The output is as follows:

+- - - - - - - - +- - - - - - +- - - - - - +- - - - - - +- - - - +| Name|age|gender|height|Grades|+--------+------+------+------+----+|  yeyz  |26|male|182|60||Ren Yingying|27|Female|172|70||Yang Guo|28|male|175|80|+--------+------+------+------+----+

Process finished with exit code 0

can be seen:

The column names can be listed through field_names

The record can be added to the table through the add_row function

You can add data records to this table through add_column

Method 2: Format the output data by loading a csv file

from prettytable import PrettyTable
from prettytable import from_csv
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

# Add through a file
'''
+- - - - - - - - +- - - - - - +- - - - - - +- - - - - - +- - - - - - +| Name|age|gender|height|fraction|+--------+------+------+------+------+|Andy Lau|56|male|165|30||Jacky Cheung|50|male|65|80||Jay Chou|38|male|170|90||dawn|40|male|180|100|+--------+------+------+------+------+'''

table1 =PrettyTable()
csv_file =open('csv_file.csv','r')
print csv_file
table1 =from_csv(csv_file)
print table1

The output is as follows:

+- - - - - - - - +- - - - - - +- - - - - - +- - - - - - +- - - - - - +| Name|age|gender|height|fraction|+--------+------+------+------+------+|Andy Lau|56|male|165|30||Jacky Cheung|50|male|65|80||Jay Chou|38|male|170|90||dawn|40|male|180|100|+--------+------+------+------+------+

Recommended Posts

Prettytable module of python
Python3 module
matplotlib of python drawing module
​Full analysis of Python module knowledge
7 features of Python3.9
Python Lesson 37-Module
Summary of common operations of Python time module
Method of installing django module in python
Python3 built-in module usage
Detailed explanation of python standard library OS module
Python3 external module use
Basic knowledge of Python (1)
09. Common modules of Python3
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
In-depth understanding of python list (LIST)
Subscripts of tuples in Python
Consolidate the foundation of Python(6)
Analysis of JS of Python crawler
Consolidate the foundation of Python(5)
Python implementation of gomoku program
Analysis of Python Sandbox Escape
Some new features of Python 3.10
Deep understanding of Python multithreading
Analysis of Python object-oriented programming
Python version of OpenCV installation
9 feature engineering techniques of Python
Python method of parameter passing
Python Faker data forgery module
Consolidate the foundation of Python (3)
Collection of Python Common Modules
The usage of wheel in python
Summary of logarithm method in Python
Use of Pandas in Python development
Detailed implementation of Python plug-in mechanism
Detailed explanation of python sequence types
Implementation of reverse traversal of python list
Python implementation of IOU calculation case
Magic methods and uses of Python
In-depth understanding of Python variable scope
Python preliminary implementation of word2vec operation
Python handles the 4 wheels of Chinese
Python calculation of information entropy example
Implementation of python selenium operation cookie
Use of numpy in Python development
Python simulation of the landlord deal
What is the use of Python
Scrapy simulation login of Python crawler
Analysis of Python conditional control statements
Black hat programming application of Python1
Detailed usage of dictionary in Python
Usage of os package in python
Several common modes of Python functions
A summary of 200 Python standard libraries!
Mongodb and python interaction of python crawler
Implementation of python student management system
There are several versions of python
Simple usage of python definition class
Summary of Python calling private attributes
Some examples of python operation redis
Example operation of python access Alipay