本文正在參加「Python主題月」,詳情查看 活動連接python
用於將 2D Python 列表轉換爲花哨的 ASCII 表的模塊。Table2Ascii 可以讓您在終端和 Discord 上顯示漂亮的表格。git
pip install table2ascii
複製代碼
from table2ascii import table2ascii
output = table2ascii(
header=["#", "G", "H", "R", "S"],
body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],
footer=["SUM", "130", "140", "135", "130"],
)
print(output)
複製代碼
輸出:github
from table2ascii import table2ascii
output = table2ascii(
body=[["Assignment", "30", "40", "35", "30"], ["Bonus", "10", "20", "5", "10"]],
first_col_heading=True,
)
print(output)
複製代碼
輸出:面試
from table2ascii import table2ascii, Alignment
output = table2ascii(
header=["#", "G", "H", "R", "S"],
body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],
first_col_heading=True,
column_widths=[5] * 5, # [5, 5, 5, 5, 5]
alignments=[Alignment.LEFT] + [Alignment.RIGHT] * 4, # First is left, remaining 4 are right
)
print(output)
複製代碼
輸出:編程
from table2ascii import table2ascii, PresetStyle
output = table2ascii(
header=["First", "Second", "Third", "Fourth"],
body=[["10", "30", "40", "35"], ["20", "10", "20", "5"]],
column_widths=[10] * 4,
style=PresetStyle.ascii_box
)
print(output)
複製代碼
輸出:小程序
查看TableStyle
更多信息和PresetStyle
示例。markdown
from table2ascii import table2ascii, TableStyle
my_style = TableStyle.from_string("*-..*||:+-+:+ *''*")
output = table2ascii(
header=["First", "Second", "Third"],
body=[["10", "30", "40"], ["20", "10", "20"], ["30", "20", "30"]],
style=my_style
)
print(output)
複製代碼
輸出:多線程
在此處查看全部預設樣式的列表。app
全部參數都是可選的。svn
選項 | 類型 | 默認 | 描述 |
---|---|---|---|
header |
List[str] |
None |
表的第一行由標題行分隔符分隔 |
body |
List[List[str]] |
None |
表格主要部分的行列表 |
footer |
List[str] |
None |
表的最後一行由標題行分隔符分隔 |
column_widths |
List[int] |
自動的 | 每列以字符爲單位的列寬列表 |
alignments |
List[int] |
所有居中 | 每列的對齊方式 (例如[Alignment.LEFT, Alignment.CENTER, Alignment.RIGHT] ) |
first_col_heading |
bool |
False |
是否在第一列後添加標題列分隔符 |
last_col_heading |
bool |
False |
是否在最後一列前添加標題列分隔符 |
運行測試(pytest)
python setup.py test
去絨(flake8):
python setup.py lint
以上就是本篇文章的所有內容,用於將 2D Python 列表轉換爲花哨的 ASCII/Unicode 表的模塊。我但願本篇博客可以幫助到你們,博主也在學習進行中,若有什麼錯誤的地方還望批評指正。若是你喜歡這篇文章並但願看到更多此類文章,能夠看看這裏(Github/Gitee) 這裏彙總了個人所有原創及做品源碼,關注我以查看更多文章。
若是你真的從這篇文章中學到了一些新東西,喜歡它,收藏它並與你的小夥伴分享。🤗最後,不要忘了❤或📑支持一下哦