This article is participating in Python Theme Month. See the link to the event for more details

table2ascii

Module for converting 2D Python lists into fancy ASCII tables. Table2Ascii allows you to display beautiful tables on your terminal and Discord.

📥 installation

pip install table2ascii
Copy the code

🧑 💻 usage

Converts a list to an ASCII table

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)
Copy the code

Output:

Sets the first or last column heading

from table2ascii import table2ascii

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

Output:

Set the column width and alignment

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)
Copy the code

Output:

Use default styles

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)
Copy the code

Output:

Define custom styles

See TableStyle for more information and an example of PresetStyle.

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)
Copy the code

Output:

🎨 Default Styles

See a list of all preset styles here.

⚙ ️ options

All parameters are optional.

options type The default describe
header List[str] None The first row of the table is separated by a header row delimiter
body List[List[str]] None A list of rows and columns in the main section of a table
footer List[str] None The last row of the table is separated by a header row delimiter
column_widths List[int] automatic A list of column widths in characters for each column
alignments List[int] All centered The alignment of each column (e.g[Alignment.LEFT, Alignment.CENTER, Alignment.RIGHT])
first_col_heading bool False Whether to add a header column separator after the first column
last_col_heading bool False Whether to add a header column separator before the last column

👨 🎨 cases

Discordant messages and emplacements

  • Displays tables nicely in the Markdown block on Discord
  • Used to build a Discord robot using Discord

Terminal output

  • As long as the uniform width font is fully supported, the table will display just fine
  • Tables make the terminal output look more professional

🧰 development

Running tests (PyTest)

python setup.py test

Fleece removal (Flake8) :

python setup.py lint

GitHub

Github.com/wanghao221/…

Quick summary – Python program to implement Morse code translator

That’s all for this article, a module for converting 2D Python lists into fancy ASCII/Unicode tables. I hope this blog has been helpful to all of you. The bloggers are still learning and I hope to criticize if there are any mistakes. If you enjoyed this article and want to see more of it, check it out here (Github/Gitee), where I have compiled all of my original works and source code, and follow me for more articles.

🥇 Python for data visualization series summary

  • Matplotlib for data visualization in Python
  • Seaborn for data visualization in Python
  • Bokeh for data visualization in Python
  • Plotly for data visualization using Python

🥈 Python utility applets

  • An AI-powered snake game using deep Q learning
  • Use Python programs to achieve Morse code translator
  • Teach you how to make a snake game in Python

🧵 Python basics

  • Python Multithreading Tutorial
  • Exception handling in Python
  • Python operators tutorial
  • Python Socket programming essentials
  • Python statements, expressions, and indentation
  • Python keywords, identifiers, and variables
  • How do I write comments and multi-line comments in Python
  • Learn about Python number and type conversions through examples
  • Python data types — from basic to advanced
  • Object-oriented programming in Python – objects, objects, and members

🚀 Skills and test questions

  • 30 Python tutorials and tips
  • 20 Python Tricks Everyone must Know
  • 100 Basic Python Interview Questions 1 (1-20)
  • 100 Basic Python Interview Questions 2 (21-40)
  • 100 Basic Python Interview Questions # 3 (41-60)
  • 100 Basic Python Interview Questions 4 (61-80)
  • 100 Basic Python Interview Questions 5 (81-100)

If you do learn something new from this post, like it, bookmark it and share it with your friends. 🤗 Finally, don’t forget ❤ or 📑 for support