Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to beautify the form with PrettyTable in python

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "python how to use PrettyTable to beautify the table". In the daily operation, I believe many people have doubts about how python uses PrettyTable to beautify the table. The editor consulted all kinds of data and sorted out a simple and easy-to-use method of operation. I hope it will be helpful for you to answer the doubt of "how python uses PrettyTable to beautify the table". Next, please follow the editor to study!

1. Install pip install PrettyTable 2. Set data import prettytable as pt# by row tb = pt.PrettyTable () tb.field_names = ['name',' age', 'height',' weight'] tb.add_row (['autofelix', 25,174,65]) tb.add_row ([' Big God', 23,164,55]) tb.add_row (['Flying Rabbit', 27,184) 69.5]) print (tb) # +-+ # | name | age | height | weight | # +-+ # | autofelix | 25 | 17465 | # | God | 23 | 16455 | # | Flying Rabbit Brother | 27 | 184 | 69.5 | # +-+ 3. Add import prettytable as pt# by column add data by row tb = pt.PrettyTable () tb.field_names = ['name' 'age',' height', 'weight'] tb.add_row ([' autofelix', 25,174,65]) tb.add_row (['Great God', 23,164,55]) tb.add_row (['Flying Rabbit', 27,184,69.5]) # add data tb.add_column by column ('sex', [' male', 'female' ) print (tb) # +-+ # | name | age | height | weight | sex | # +-+ # | autofelix | 25 | 174 | 65 | Male | # | God | 23 | 164 | 55 | female | # | Brother Flying Rabbit | 27 | 184 | 69.5 | male | # +-+ 4, Output style

MSWORD_FRIENDLY:MSWORD_FRIENDLY output style

PLAIN_COLUMNS:PLAIN_COLUMNS output style

RANDOM: random output style per time

DEFAULT: default output style

Import prettytable as pt# adds data by row tb = pt.PrettyTable () tb.field_names = ['name',' age', 'height',' weight'] tb.add_row (['autofelix', 25,174,65]) tb.add_row ([' Big God', 23,164,55]) tb.add_row (['Flying Rabbit', 27,184) ) # style tb.set_style (pt.MSWORD_FRIENDLY) print (tb) # | name | age | height | weight | # | autofelix | 25 | 17465 | # | God | 23 | 164,55 | # | Flying Rabbit | 27 | 184,69.5 | 5. Get the string import prettytable as pt# to add data by line tb = pt.PrettyTable () tb.field_names = ['name',' age', 'height' 'weight'] tb.add_row ([' autofelix', 25,174,65]) tb.add_row (['Great God', 23,164,55]) tb.add_row (['Flying Rabbit', 27,184,69.5]) # do not print Get the table string S1 = tb.get_string () print (S1) # +-+ # | name | age | height | weight | # +-+ # | autofelix | 25 | 17465 | # | God | | 23 | 164,55 | # | Flying Rabbit | 27 | 184,69.5 | # +-+ # or you can only get the specified column or row S2 = tb.get_string (fields= ['name') | 'age'], start=1, end=4) print (S2) # +-+-+ # | name | age | # +-+-+ # | God | 23 | # | Flying Rabbit | 27 | # +-+-+ 6. Table style setting import prettytable as pt# add data by row tb = pt.PrettyTable () tb.field_names = [' name' 'age',' height', 'weight'] tb.add_row ([' autofelix', 25,174,65]) tb.add_row (['God', 23,164,55]) tb.add_row (['Flying Rabbit', 27,184) ) # set left alignment tb.align = 'lager # set digital output format tb.float_format =' 2.2'# set border connector to'* "tb.junction_char ='*'# set sort mode tb.sortby = 'age'# set left not to fill in blank characters tb.left_padding_width =" do not display border # tb.border = "modify border separation Fu tb.Tibetan _ char ='+ 'print (tb) # * + * # | name | age | height | weight | # * + * # | God | 23 | 164,55 | autofelix | 25 | 65 | # | Flying Rabbit | 27 | 184,69.50 | # * + * 7, Output to HTMLimport prettytable as pt# add data by line tb = pt.PrettyTable () tb.field_names = ['name' 'age',' height', 'weight'] tb.add_row ([' autofelix', 25,174,65]) tb.add_row (['God', 23,164,55]) tb.add_row (['Flying Rabbit', 27,184) ) # output HTML code s = tb.get_html_string () print (s) # name# age# height# weight# autofelix# 2 "17" 6 "God # 2" 16 "5" Flying Rabbit # 27 # 18 "69.8, copy import prettytable as pt# to add data by line tb = pt.PrettyTable () tb.field_names = ['name' 'age',' height', 'weight'] tb.add_row ([' autofelix', 25,174,65]) tb.add_row (['God', 23,164,55]) tb.add_row (['Flying Rabbit', 27,184) ) tb.horizontal_char ='. Tb2 = tb.copy () tb.align = 'l'tb2.align =' r'print (tb) print (tb2) # +. + # | name | age | height | weight | # +. + . + # | autofelix | 25 | 174 | 65 | # | God | 23 | 164,55 | # | Flying Rabbit | 27 | 184,69.5 | # +. + # +. + # | name | age | height | weight | # +. + # | autofelix | 25 | 174,65 | # | God | 23 | 164,55 | # | Flying Rabbit | 27 | 184,69.5 | # +. The study on "how python uses PrettyTable to beautify the table" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report