用於將 2D Python 列表轉換爲花哨的 ASCII/Unicode 表的模塊

本文正在參加「Python主題月」,詳情查看 活動連接python

table2ascii

用於將 2D Python 列表轉換爲花哨的 ASCII 表的模塊。Table2Ascii 可以讓您在終端和 Discord 上顯示漂亮的表格。git

📥 安裝

pip install table2ascii
複製代碼

image.png

🧑‍💻 用法

將列表轉換爲 ASCII 表

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

image.png

設置第一個或最後一個列標題

from table2ascii import table2ascii

output = table2ascii(
    body=[["Assignment", "30", "40", "35", "30"], ["Bonus", "10", "20", "5", "10"]],
    first_col_heading=True,
)

print(output)
複製代碼

輸出:面試

image.png

設置列寬和對齊方式

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)
複製代碼

輸出:編程

image.png

使用預設樣式

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)
複製代碼

輸出:小程序

image.png

定義自定義樣式

查看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)
複製代碼

輸出:多線程

image.png

🎨 預設樣式

在此處查看全部預設樣式的列表。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 是否在最後一列前添加標題列分隔符

👨‍🎨 用例

不和諧消息和嵌入

  • 在 Discord 上的 Markdown 代碼塊中很好地顯示錶格
  • 用於使用Discord.py製做 Discord 機器人

image.png

終端輸出

  • 只要徹底支持等寬字體,表格就會很好地顯示
  • 表格讓終端輸出看起來更專業

image.png

🧰 發展

運行測試(pytest)

python setup.py test

去絨(flake8):

python setup.py lint

GitHub

github.com/wanghao221/…

快速總結——Python 程序實現摩斯密碼翻譯器

以上就是本篇文章的所有內容,用於將 2D Python 列表轉換爲花哨的 ASCII/Unicode 表的模塊。我但願本篇博客可以幫助到你們,博主也在學習進行中,若有什麼錯誤的地方還望批評指正。若是你喜歡這篇文章並但願看到更多此類文章,能夠看看這裏(Github/Gitee) 這裏彙總了個人所有原創及做品源碼,關注我以查看更多文章。

🥇 Python 進行數據可視化系列彙總

🥈 Python實用小程序

🧵 Python基礎知識

🚀 技巧和麪試題

若是你真的從這篇文章中學到了一些新東西,喜歡它,收藏它並與你的小夥伴分享。🤗最後,不要忘了❤或📑支持一下哦

相關文章
相關標籤/搜索