__________     __ __     __  _______    ________
  / ____/ __ \   / // /    / / / /  _/ |  / / ____/
 / / __/ / / /  / // /_   / /_/ // / | | / / __/
/ /_/ / /_/ /  /__  __/  / __  // /  | |/ / /___
\____/\____/     /_/    /_/ /_/___/  |___/_____/

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

Create readable CLI table using Python's PrettyTable library

BY: @rohancmr | CREATED: Jan. 10, 2018, 6:30 p.m. | VOTES: 12 | PAYOUT: $31.65 | [ VOTE ]

PrettyTable is a python library designed to display tabular data in a visually appealing ASCII table. It generates tables similar to one used in PostgreSQL shell psql.

Features of PrettyTable:

Installation

PrettyTable can be installed using pip. In Windows, pip is available under "script" directory and in linux under "bin" directory of Python's installation path.

Command to install PrettyTable using pip :

pip install prettytable

Once it is installed, check if we can import prettytable using below command :

import prettytable

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1515607431/dfsqoa3srmfzb5vthirc.gif]

Example 1 : Create a table using add_row() method

from prettytable import PrettyTable

table = PrettyTable(['Coin', 'Price', 'High', 'Low'])

table.add_row(['BTC', '14525.00 USD', '15355.00 USD', '13755.00 USD'])
table.add_row(['ETH', '1191.00 USD', '1250.00 USD', '965.18 USD'])
table.add_row(['XRP', '2.25 USD', '2.49 USD', '1.90 USD'])
table.add_row(['LTC', '247.72 USD', '258.04 USD', '230.18 USD'])
table.add_row(['MIOTA', '3.64 USD', '3.95 USD', '3.15 USD'])

print(table)

Full Code

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1515599461/enbqb74wz3xs5ilspzja.png]

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1515600696/tkqqlu3enacepe3u0vbi.gif]

Example 2 : Create a table from a CSV file :

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1515605913/pyfte4e7gxxuipnwkact.png]

from prettytable import from_csv

Full Code :

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1515606022/brxe60mzmcqsjbs8hied.png]

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1515605721/uayfaly6icshdnzgofoe.gif]

Example 3 : Create table from a html file.

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1515606182/ql6avlhgszdmqggdiv35.png]

from prettytable import from_html_one

with open('data.html', 'r') as f:

html_data = f.read()

table = from_html_one(html_data)

table.align = 'l'

print(table)

Full Code

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1515606764/afuosscunmll1thte2ng.png]

[IMAGE: https://res.cloudinary.com/hpiynhbhq/image/upload/v1515606813/jitpsi8fzdtrv49ops2w.gif]

Conclusion

Using PrettyTable python library we can quickly and easily represent tabular data in nice CLI tables. Apart from above examples, it can be used for printing range of rows or selected columns. It can also be used to display table by sorting a given column.

If you have any questions, comments or have used PrettyTable before, I'd would love to hear from you in comment section.

Posted on Utopian.io - Rewarding Open Source Contributors

TAGS: [ #utopian-io ] [ #python ] [ #programming ] [ #prettytable ] [ #cli-tables ]

Replies

@funlover1122 | Jan. 10, 2018, 6:31 p.m. | Votes: 0 | [ VOTE ]

Nice post i like it
I follow you and you follow mee back
i vote you vote me back

@rohancmr | Jan. 10, 2018, 6:33 p.m. | Votes: 0 | [ VOTE ]

Thank you.

@rohancmr | Jan. 10, 2018, 6:34 p.m. | Votes: 0 | [ VOTE ]

@originalworks

@originalworks | Jan. 11, 2018, 1:11 a.m. | Votes: 0 | [ VOTE ]

The @OriginalWorks bot has determined this post by @rohancmr to be original material and upvoted it!
[IMAGE: https://steemitimages.com/DQmaBi37A5oTnQ9NBLH8YU4jpvhhmFauyvgg3YRrEJwskM9/ezgif.com-resize.gif]

To call @OriginalWorks, simply reply to any post with @originalworks or !originalworks in your message!
Please note that this is a BETA version. Feel free to leave a reply if you feel this is an error to help improve accuracy.

@gudivadamani7 | Jan. 10, 2018, 6:38 p.m. | Votes: 0 | [ VOTE ]

Nice post @rohancmr. Impressive tool.

@luciancovaci | Jan. 11, 2018, 5:47 a.m. | Votes: 0 | [ VOTE ]

Good how to. Upvote and Follow!

@rohancmr | Jan. 11, 2018, 11:41 a.m. | Votes: 0 | [ VOTE ]

Just click (^) icon to upvote and click my user ID "rohancmr" and then click the follow link.

@luciancovaci | Jan. 11, 2018, 11:45 a.m. | Votes: 0 | [ VOTE ]

Already done !

@shreyasgune | Jan. 11, 2018, 11:11 a.m. | Votes: 0 | [ VOTE ]

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

@utopian-io | Jan. 12, 2018, 12:23 p.m. | Votes: 1 | [ VOTE ]

Hey @rohancmr I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • This is your first accepted contribution here in Utopian. Welcome!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
- Vote for my Witness With SteemConnect
- Proxy vote to Utopian Witness with SteemConnect
- Or vote/proxy on Steemit Witnesses

[IMAGE: https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.gif]

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>