Cách dễ nhất để tạo các bảng trong Python là sử dụng hàm Table () từ thư viện bảng.tablulate() function from the tabulate library.
Để sử dụng chức năng này, trước tiên chúng ta phải cài đặt thư viện bằng PIP:
pip install tabulateSau đó chúng tôi có thể tải thư viện:
from tabulate import tabulateSau đó, chúng ta có thể sử dụng cú pháp cơ bản sau để tạo bảng:
print(tabulate(data, headers=col_names, tablefmt="grid", showindex="always"))Các ví dụ sau đây cho thấy cách sử dụng chức năng này trong thực tế.
Ví dụ 1: Tạo bảng với các tiêu đề
Mã sau đây cho thấy cách tạo một bảng cơ bản với các tiêu đề:
#create data data = [["Mavs", 99], ["Suns", 91], ["Spurs", 94], ["Nets", 88]] #define header names col_names = ["Team", "Points"] #display table print(tabulate(data, headers=col_names)) Team Points ------ -------- Mavs 99 Suns 91 Spurs 94 Nets 88Ví dụ 2: Tạo bảng với lưới ưa thích
Mã sau đây cho thấy cách tạo bảng với các tiêu đề và lưới ưa thích:
#create data data = [["Mavs", 99], ["Suns", 91], ["Spurs", 94], ["Nets", 88]] #define header names col_names = ["Team", "Points"] #display table print(tabulate(data, headers=col_names, tablefmt="fancy_grid")) ╒════════╤══════════╕ │ Team │ Points │ ╞════════╪══════════╡ │ Mavs │ 99 │ ├────────┼──────────┤ │ Suns │ 91 │ ├────────┼──────────┤ │ Spurs │ 94 │ ├────────┼──────────┤ │ Nets │ 88 │ ╘════════╧══════════╛Lưu ý rằng đối số của bảng chấp nhận một số tùy chọn khác nhau bao gồm:tablefmt argument accepts several different options including:
- Lưới
- fancy_grid
- đường ống
- đẹp
- giản dị
Tham khảo tài liệu Tabulation để biết danh sách đầy đủ các định dạng bảng tiềm năng.
Ví dụ 3: Tạo bảng với cột chỉ mục
Mã sau đây cho thấy cách tạo bảng với các tiêu đề, lưới ưa thích và cột chỉ mục:
#create data data = [["Mavs", 99], ["Suns", 91], ["Spurs", 94], ["Nets", 88]] #define header names col_names = ["Team", "Points"] #display table print(tabulate(data, headers=col_names, tablefmt="fancy_grid", showindex="always")) ╒════╤════════╤══════════╕ │ │ Team │ Points │ ╞════╪════════╪══════════╡ │ 0 │ Mavs │ 99 │ ├────┼────────┼──────────┤ │ 1 │ Suns │ 91 │ ├────┼────────┼──────────┤ │ 2 │ Spurs │ 94 │ ├────┼────────┼──────────┤ │ 3 │ Nets │ 88 │ ╘════╧════════╧══════════╛Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luậnUsing Tabulate module
Trong bài viết này, chúng tôi sẽ thảo luận về cách làm một bảng trong Python. Python cung cấp hỗ trợ rộng lớn cho các thư viện có thể được sử dụng để tạo ra các mục đích khác nhau. Trong bài viết này, chúng tôi sẽ nói về hai mô -đun như vậy có thể được sử dụng để tạo bảng.tabulate() method is a method present in the tabulate module which creates a text-based table output inside the python program using any given inputs. It can be installed using the below command
pip install tabulatePhương pháp 1: Sử dụng mô -đun Tabulation
Phương thức Tabulation () là một phương thức có trong mô-đun bảng tạo ra đầu ra bảng dựa trên văn bản bên trong chương trình Python bằng cách sử dụng bất kỳ đầu vào nào. Nó có thể được cài đặt bằng lệnh dưới đây
Python3
from tabulate from tabulate import tabulate 0 from tabulate import tabulate 1
from tabulate import tabulate 2from tabulate import tabulate 3 from tabulate import tabulate 4
from tabulate import tabulate 5from tabulate import tabulate 4from tabulate import tabulate 7from tabulate import tabulate 8from tabulate import tabulate 9print(tabulate(data, headers=col_names, tablefmt="grid", showindex="always")) 0
from tabulate import tabulate 5from tabulate import tabulate 4print(tabulate(data, headers=col_names, tablefmt="grid", showindex="always")) 3from tabulate import tabulate 8print(tabulate(data, headers=col_names, tablefmt="grid", showindex="always")) 5print(tabulate(data, headers=col_names, tablefmt="grid", showindex="always")) 0
from tabulate import tabulate 5from tabulate import tabulate 4print(tabulate(data, headers=col_names, tablefmt="grid", showindex="always")) 9from tabulate import tabulate 8#create data data = [["Mavs", 99], ["Suns", 91], ["Spurs", 94], ["Nets", 88]] #define header names col_names = ["Team", "Points"] #display table print(tabulate(data, headers=col_names)) Team Points ------ -------- Mavs 99 Suns 91 Spurs 94 Nets 88 1print(tabulate(data, headers=col_names, tablefmt="grid", showindex="always")) 0
#create data data = [["Mavs", 99], ["Suns", 91], ["Spurs", 94], ["Nets", 88]] #define header names col_names = ["Team", "Points"] #display table print(tabulate(data, headers=col_names)) Team Points ------ -------- Mavs 99 Suns 91 Spurs 94 Nets 88 3from tabulate import tabulate 4#create data data = [["Mavs", 99], ["Suns", 91], ["Spurs", 94], ["Nets", 88]] #define header names col_names = ["Team", "Points"] #display table print(tabulate(data, headers=col_names)) Team Points ------ -------- Mavs 99 Suns 91 Spurs 94 Nets 88 5from tabulate import tabulate 8#create data data = [["Mavs", 99], ["Suns", 91], ["Spurs", 94], ["Nets", 88]] #define header names col_names = ["Team", "Points"] #display table print(tabulate(data, headers=col_names)) Team Points ------ -------- Mavs 99 Suns 91 Spurs 94 Nets 88 7#create data data = [["Mavs", 99], ["Suns", 91], ["Spurs", 94], ["Nets", 88]] #define header names col_names = ["Team", "Points"] #display table print(tabulate(data, headers=col_names)) Team Points ------ -------- Mavs 99 Suns 91 Spurs 94 Nets 88 8
#create data data = [["Mavs", 99], ["Suns", 91], ["Spurs", 94], ["Nets", 88]] #define header names col_names = ["Team", "Points"] #display table print(tabulate(data, headers=col_names)) Team Points ------ -------- Mavs 99 Suns 91 Spurs 94 Nets 88 8
#create data data = [["Mavs", 99], ["Suns", 91], ["Spurs", 94], ["Nets", 88]] #define header names col_names = ["Team", "Points"] #display table print(tabulate(data, headers=col_names, tablefmt="fancy_grid")) ╒════════╤══════════╕ │ Team │ Points │ ╞════════╪══════════╡ │ Mavs │ 99 │ ├────────┼──────────┤ │ Suns │ 91 │ ├────────┼──────────┤ │ Spurs │ 94 │ ├────────┼──────────┤ │ Nets │ 88 │ ╘════════╧══════════╛0from tabulate import tabulate 3 from tabulate import tabulate 4#create data data = [["Mavs", 99], ["Suns", 91], ["Spurs", 94], ["Nets", 88]] #define header names col_names = ["Team", "Points"] #display table print(tabulate(data, headers=col_names, tablefmt="fancy_grid")) ╒════════╤══════════╕ │ Team │ Points │ ╞════════╪══════════╡ │ Mavs │ 99 │ ├────────┼──────────┤ │ Suns │ 91 │ ├────────┼──────────┤ │ Spurs │ 94 │ ├────────┼──────────┤ │ Nets │ 88 │ ╘════════╧══════════╛3from tabulate import tabulate 8#create data data = [["Mavs", 99], ["Suns", 91], ["Spurs", 94], ["Nets", 88]] #define header names col_names = ["Team", "Points"] #display table print(tabulate(data, headers=col_names, tablefmt="fancy_grid")) ╒════════╤══════════╕ │ Team │ Points │ ╞════════╪══════════╡ │ Mavs │ 99 │ ├────────┼──────────┤ │ Suns │ 91 │ ├────────┼──────────┤ │ Spurs │ 94 │ ├────────┼──────────┤ │ Nets │ 88 │ ╘════════╧══════════╛5#create data data = [["Mavs", 99], ["Suns", 91], ["Spurs", 94], ["Nets", 88]] #define header names col_names = ["Team", "Points"] #display table print(tabulate(data, headers=col_names)) Team Points ------ -------- Mavs 99 Suns 91 Spurs 94 Nets 88 8
Các
Output:
Ví dụ 2
Python3
from tabulate from tabulate import tabulate 0 from tabulate import tabulate 1
from tabulate import tabulate 2from tabulate import tabulate 3 from tabulate import tabulate 4
from tabulate import tabulate 5from tabulate import tabulate 4pip install tabulate3from tabulate import tabulate 8pip install tabulate5from tabulate import tabulate 8pip install tabulate7print(tabulate(data, headers=col_names, tablefmt="grid", showindex="always")) 0
Các
#create data data = [["Mavs", 99], ["Suns", 91], ["Spurs", 94], ["Nets", 88]] #define header names col_names = ["Team", "Points"] #display table print(tabulate(data, headers=col_names)) Team Points ------ -------- Mavs 99 Suns 91 Spurs 94 Nets 88 3from tabulate import tabulate 4pip install prettytable 9from tabulate import tabulate 8from1from tabulate import tabulate 8from3#create data data = [["Mavs", 99], ["Suns", 91], ["Spurs", 94], ["Nets", 88]] #define header names col_names = ["Team", "Points"] #display table print(tabulate(data, headers=col_names)) Team Points ------ -------- Mavs 99 Suns 91 Spurs 94 Nets 88 8
#create data data = [["Mavs", 99], ["Suns", 91], ["Spurs", 94], ["Nets", 88]] #define header names col_names = ["Team", "Points"] #display table print(tabulate(data, headers=col_names)) Team Points ------ -------- Mavs 99 Suns 91 Spurs 94 Nets 88 8
#create data data = [["Mavs", 99], ["Suns", 91], ["Spurs", 94], ["Nets", 88]] #define header names col_names = ["Team", "Points"] #display table print(tabulate(data, headers=col_names, tablefmt="fancy_grid")) ╒════════╤══════════╕ │ Team │ Points │ ╞════════╪══════════╡ │ Mavs │ 99 │ ├────────┼──────────┤ │ Suns │ 91 │ ├────────┼──────────┤ │ Spurs │ 94 │ ├────────┼──────────┤ │ Nets │ 88 │ ╘════════╧══════════╛7from7
Output:
Phương pháp 2: Sử dụng mô -đun đẹp mắtUsing PrettyTable module
Lớp đẹp bên trong thư viện đẹp mắt được sử dụng để tạo các bảng quan hệ trong Python. Nó có thể được cài đặt bằng lệnh dưới đây.
pip install prettytableExample:
Python3
from from9from tabulate import tabulate 0 tabulate 1
tabulate 2from tabulate import tabulate 3 tabulate 4tabulate 5from tabulate import tabulate 8tabulate 7from tabulate import tabulate 8tabulate 9from tabulate import tabulate 8from tabulate import tabulate 01
from tabulate import tabulate 03from tabulate import tabulate 04from tabulate import tabulate 8from tabulate import tabulate 06from tabulate import tabulate 8from tabulate import tabulate 08from tabulate import tabulate 8from tabulate import tabulate 10from tabulate import tabulate 02
from tabulate import tabulate 03from tabulate import tabulate 13from tabulate import tabulate 8from tabulate import tabulate 06from tabulate import tabulate 8from tabulate import tabulate 17from tabulate import tabulate 8from tabulate import tabulate 19from tabulate import tabulate 02
from tabulate import tabulate 03from tabulate import tabulate 22from tabulate import tabulate 8from tabulate import tabulate 06from tabulate import tabulate 8from tabulate import tabulate 26from tabulate import tabulate 8from tabulate import tabulate 28from tabulate import tabulate 02
from tabulate import tabulate 03from tabulate import tabulate 31from tabulate import tabulate 8from tabulate import tabulate 06from tabulate import tabulate 8from tabulate import tabulate 35from tabulate import tabulate 8from tabulate import tabulate 37from tabulate import tabulate 02
from tabulate import tabulate 03from tabulate import tabulate 40from tabulate import tabulate 8from tabulate import tabulate 06from tabulate import tabulate 8from tabulate import tabulate 26from tabulate import tabulate 8from tabulate import tabulate 46from tabulate import tabulate 02
from tabulate import tabulate 03from tabulate import tabulate 49from tabulate import tabulate 8from tabulate import tabulate 06from tabulate import tabulate 8from tabulate import tabulate 08from tabulate import tabulate 8from tabulate import tabulate 55from tabulate import tabulate 02
Các
#create data data = [["Mavs", 99], ["Suns", 91], ["Spurs", 94], ["Nets", 88]] #define header names col_names = ["Team", "Points"] #display table print(tabulate(data, headers=col_names, tablefmt="fancy_grid")) ╒════════╤══════════╕ │ Team │ Points │ ╞════════╪══════════╡ │ Mavs │ 99 │ ├────────┼──────────┤ │ Suns │ 91 │ ├────────┼──────────┤ │ Spurs │ 94 │ ├────────┼──────────┤ │ Nets │ 88 │ ╘════════╧══════════╛7from tabulate import tabulate 67
Output: