Python Forum
printing text tables with consistent width
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
printing text tables with consistent width
#4
python-tabulate to take the more lazy way than @ichabod801 Wink
>>> from tabulate import tabulate

>>> knights = [['Sir Lancelot', 'The Holy Grail', 'Blue'],
    ['Sir Robin', 'The Holy Grail', "I don't know that."],
    ['Sir Galahad', 'The Grail', 'Blue. No, yel...'],
    ['King Arthur', 'The Holy Grail', 'What do you mean?']]

>>> print(tabulate(knights))
------------  --------------  ------------------
Sir Lancelot  The Holy Grail  Blue
Sir Robin     The Holy Grail  I don't know that.
Sir Galahad   The Grail       Blue. No, yel...
King Arthur   The Holy Grail  What do you mean?
------------  --------------  ------------------

>>> print(tabulate(knights, tablefmt="pipe"))
|:-------------|:---------------|:-------------------|
| Sir Lancelot | The Holy Grail | Blue               |
| Sir Robin    | The Holy Grail | I don't know that. |
| Sir Galahad  | The Grail      | Blue. No, yel...   |
| King Arthur  | The Holy Grail | What do you mean?  |

>>> print(tabulate(knights, tablefmt="fancy_grid"))
╒══════════════╤════════════════╤════════════════════╕
│ Sir Lancelot │ The Holy Grail │ Blue               │
├──────────────┼────────────────┼────────────────────┤
│ Sir Robin    │ The Holy Grail │ I don't know that. │
├──────────────┼────────────────┼────────────────────┤
│ Sir Galahad  │ The Grail      │ Blue. No, yel...   │
├──────────────┼────────────────┼────────────────────┤
│ King Arthur  │ The Holy Grail │ What do you mean?  │
╘══════════════╧════════════════╧════════════════════╛
Reply


Messages In This Thread
RE: printing text tables with consistent width - by snippsat - Jun-30-2018, 04:42 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code to set column width 1418 11 1,844 Jan-20-2024, 07:20 AM
Last Post: Pedroski55
  Fixed colum width for rowLabels i Matplotlib pandabay 0 506 Jun-10-2023, 03:40 PM
Last Post: pandabay
  Setup host for consistent data transfer to client via TCP Gustav97 0 747 Jun-27-2022, 07:33 PM
Last Post: Gustav97
  Consistent error Led_Zeppelin 1 1,737 Dec-20-2021, 01:39 AM
Last Post: snippsat
  width of Unicode character Skaperen 6 2,923 Sep-27-2021, 12:41 AM
Last Post: Skaperen
  image.thumbnail(width, height) not working PCesarano 2 3,577 Apr-08-2021, 06:09 PM
Last Post: PCesarano
  Error printing colored text julio2000 0 1,543 Feb-02-2020, 07:04 PM
Last Post: julio2000
  How can I get the width of a string in Python? aquerci 14 16,707 May-27-2019, 06:00 PM
Last Post: heiner55
  fixed width numbers Skaperen 15 9,043 May-27-2019, 09:42 AM
Last Post: Skaperen
  Printing lines in a basic text file Drone4four 6 4,065 Aug-16-2018, 03:10 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020