它是一个Python模块,可以帮助我们在终端上打印表格。它是用于以 ASCII 代码读写文本表的基本 Python 模块之一。它的目的是使接口尽可能相似,例如数据集Python 中的模块。这文本表模块支持固定大小的表(其中列大小是预先确定的)和dynamic-size表(其中可以添加或删除列)。
安装:
pip install texttable
分步方法:
- 导入所需的模块。
Python3
# Import required module
import texttable
- 创建一个对象texttable()
Python3
# Creating object
tableObj = texttable.Texttable(self,max width)
# max_width must be an integer,whose value is maximum width of the table
# if set to 0, size is unlimited (self adjustible according to text inside cell),
# therefore cells won't be wrapped so it's recommended to use 0
- 采用set_cols_align()方法 创建列。
Python3
# Creating columns
tableObj.set_cols_align(["l", "l", "r", "c"])
# Set the desired columns alignment:
# "l" refers to column flushed left
# "c" refers to column centered
# "r" refers to column flushed right
- 采用set_cols_dtype()设置每列的数据类型。但是,此步骤是可选的。
Python3
# Set datatype
tableObj.set_cols_dtype(["t", "i", "f", "a"])
# texttable objects supports five types of data types:
# "t" refers to text
# "f" refers to decimal
# "e" refers to exponent
# "i" refers to integer
# "a" refers to automatic
- 采用set_cols_valign()调整列。
Python3
# Adjust Columns
tableObj.set_cols_valign(["t", "t", "m", "b"])
# Set the desired columns vertical alignment the elements of the
# array should be either "t", "m" or "b":
# "t" refers to column aligned on the top of the cell
# "m" refers to column aligned on the middle of the cell
# "b" refers to column aligned on the bottom of the cell
- 采用add_rows()向表中插入行的方法
Python3
# Adding rows
table.add_rows([
["Text_Heading", "Int_Heading", "Float_Heading", "Auto_Heading"],
["Data1", 9, 1.23456789, "GFG"],
["Data2", 1, 9.87654321, "g4g"],
])
# add_rows(self, rows, header=True):
# The 'rows' argument can be either an iterator returning arrays, or a
# by-dimensional array.
# 'header' specifies if the first row should be used as the header of the table
- 采用draw()显示表格的方法。
Python3
print(tableObj.draw())
表格说明如下:
下面是基于上述方法的程序:
Python3
# Import required module
import texttable
# Create texttable object
tableObj = texttable.Texttable()
# Set columns
tableObj.set_cols_align(["l", "r", "c"])
# Set datatype of each column
tableObj.set_cols_dtype(["a", "i", "t"])
# Adjust columns
tableObj.set_cols_valign(["t", "m", "b"])
# Insert rows
tableObj.add_rows([
["ORGANIZATION", "ESTABLISHED", "CEO"],
["Google", 1998, "Sundar Pichai"],
["Microsoft", 1975, "Satya Nadella"],
["Nokia", 1865, "Rajeev Suri"],
["Geeks for Geeks", 2008, "Sandeep Jain"],
["HackerRank", 2007, "Vivek Ravisankar"]
])
# Display table
print(tableObj.draw())
输出:
相关用法
- Python TextCalendar formatmonth()用法及代码示例
- Python TextCalendar formatyear()用法及代码示例
- Python TextCalendar prmonth()用法及代码示例
- Python TextCalendar pryear()用法及代码示例
- Python TextBlob.correct()用法及代码示例
- Python TextBlob.noun_phrases()用法及代码示例
- Python TextBlob.sentiment()用法及代码示例
- Python TextBlob.Word.spellcheck()用法及代码示例
- Python TextBlob.word_counts()用法及代码示例
- Python Text转Speech用法及代码示例
- Python TextaCy用法及代码示例
- Python Tensorflow abs()用法及代码示例
- Python Tensorflow acos()用法及代码示例
- Python Tensorflow acosh()用法及代码示例
- Python Tensorflow asin()用法及代码示例
- Python Tensorflow asinh()用法及代码示例
- Python Tensorflow atan()用法及代码示例
- Python Tensorflow atanh()用法及代码示例
- Python Tensorflow cos()用法及代码示例
- Python Tensorflow cosh()用法及代码示例
- Python Tensorflow exp()用法及代码示例
- Python Tensorflow log()用法及代码示例
- Python Tensorflow logical_and()用法及代码示例
- Python Tensorflow logical_not()用法及代码示例
- Python Tensorflow logical_or()用法及代码示例
注:本文由纯净天空筛选整理自佚名大神的英文原创作品 TextTable module in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。