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

The usage of Python practical Library PrettyTable

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "the usage of Python practical library PrettyTable". In daily operation, I believe many people have doubts about the usage of Python practical library PrettyTable. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the usage of Python practical library PrettyTable"! Next, please follow the editor to study!

PrettyTable installation

You can easily install PrettyTable using pip, as follows:

Pip install PrettyTable

Example of using PrettyTable

Instructions for using PrettyTable are available on github. The link is as follows: https://github.com/dprince/python-prettytable

The following is a specific example of use:

Import prettytable as pt

Add data by row

Tb = pt.PrettyTable () tb.field_names = ["City name", "Area", "Population", "Annual Rainfall"] tb.add_row (["Adelaide", 1295, 1158259, 600.5]) tb.add_row (["Brisbane", 5905, 1857594, 1146.4]) tb.add_row (["Darwin", 112, 120900, 1714.7]) tb.add_row (["Hobart", 1357, 205556619.5]) print (tb)

+-+

| | City name | Area | Population | Annual Rainfall | |

+-+

| | Adelaide | 1295 | 1158259 | 600.5 | |

| | Brisbane | 5905 | 1857594 | 1146.4 | |

| | Darwin | 112 | 120900 | 1714.7 | |

| | Hobart | 1357 | 205556 | 619.5 | |

+-+

Add data by column

Tb.add_column ('index', [1, 2, 2, 3, 4]) print (tb)

+-+

| | City name | Area | Population | Annual Rainfall | index | |

+-+

| | Adelaide | 1295 | 1158259 | 600.5 | 1 | |

| | Brisbane | 5905 | 1857594 | 1146.4 | 2 |

| | Darwin | 112 | 120900 | 1714.7 | 3 | |

| | Hobart | 1357 | 205556 | 619.5 | 4 |

+-+

Use different output styles

Tb.set_style (pt.MSWORD_FRIENDLY) print ('--style:MSWORD_FRIENDLY -') print (tb) tb.set_style (pt.PLAIN_COLUMNS) print ('- style:PLAIN_COLUMNS -') print (tb)

Random style, different each time.

Tb.set_style (pt.RANDOM) print ('--style:MSWORD_FRIENDLY -') print (tb) tb.set_style (pt.DEFAULT) print ('- style:DEFAULT -') print (tb)

-style:MSWORD_FRIENDLY-

| | City name | Area | Population | Annual Rainfall | |

| | Adelaide | 1295 | 1158259 | 600.5 | |

| | Brisbane | 5905 | 1857594 | 1146.4 | |

| | Darwin | 112 | 120900 | 1714.7 | |

| | Hobart | 1357 | 205556 | 619.5 | |

-style:PLAIN_COLUMNS-

City name Area Population Annual Rainfall

Adelaide 1295 1158259 600.5

Brisbane 5905 1857594 1146.4

Darwin 112 120900 1714.7

Hobart 1357 205556 619.5

-style:MSWORD_FRIENDLY-

@ Adelaide 1295 1158259 600.5 @

@ Brisbane 5905 1857594 1146.4 @

@ Darwin 112 120900 1714.7 @

@ Hobart 1357 205556 619.5 @

-style:DEFAULT-

+-+

| | City name | Area | Population | Annual Rainfall | |

+-+

| | Adelaide | 1295 | 1158259 | 600.5 | |

| | Brisbane | 5905 | 1857594 | 1146.4 | |

| | Darwin | 112 | 120900 | 1714.7 | |

| | Hobart | 1357 | 205556 | 619.5 | |

+-+

Do not print, get the form string

S = tb.get_string () print (s)

You can get only the specified column or row

S = tb.get_string (fields= ["City name", "Population"], start=1,end=4) print (s)

+-+

| | City name | Area | Population | Annual Rainfall | |

+-+

| | Adelaide | 1295 | 1158259 | 600.5 | |

| | Brisbane | 5905 | 1857594 | 1146.4 | |

| | Darwin | 112 | 120900 | 1714.7 | |

| | Hobart | 1357 | 205556 | 619.5 | |

+-+

+-+ +

| | City name | Population |

+-+ +

| | Brisbane | 1857594 | |

| | Darwin | 120900 | |

| | Hobart | 205556 | |

+-+ +

Customize the table output style

Set left alignment

Tb.align ='l'

Set the digital output format

Tb.float_format = "2.2"

Set the border connector to'* "

Tb.junction_char = "*"

Set the sort mode

Tb.sortby = "City name"

Set the left side not to fill with white space characters

Tb.left_padding_width = 0print (tb)

*-*

| | City name | Area | Population | Annual Rainfall | |

*-*

| | Adelaide | 1295 | 1158259 | 600.50 | |

| | Brisbane | 5905 | 1857594 | 1146.40 | |

| | Darwin | 112 | 120900 | 1714.70 | |

| | Hobart | 1357 | 205556 | 619.50 | |

*-*

Do not display the border

Tb.border = 0print (tb)

Modify the border delimiter

Tb.set_style (pt.DEFAULT) tb.horizontal_char ='+ 'print (tb)

City name Area Population Annual Rainfall

Adelaide 1295 1158259 600.50

Brisbane 5905 1857594 1146.40

Darwin 112 120900 1714.70

Hobart 1357 205556 619.50

+ + +

| | City name | Area | Population | Annual Rainfall | |

+ + +

| | Adelaide | 1295 | 1158259 | 600.50 | |

| | Brisbane | 5905 | 1857594 | 1146.40 | |

| | Darwin | 112 | 120900 | 1714.70 | |

| | Hobart | 1357 | 205556 | 619.50 | |

+ + +

Prettytable also supports outputting HTML code.

S = tb.get_html_string () print (s) City nameAreaPopulationAnnual RainfallAdelaide12951158259600.50Brisbane590518575941146.40Darwin1121209001714.70Hobart1357205556619.50

Copy objects using the copy method

Tb.set_style (pt.DEFAULT) tb.horizontal_char ='. Tb2 = tb.copy () tb.align = 'l'tb2.align =' r'print (tb) print (tb2)

Directly assign the value, and you get the index.

Tb.horizontal_char ='- 'tb.aliign =' l'tb3 = tbtb3.align = 'r'print (tb) print (tb3)

+. +

| | City name | Area | Population | Annual Rainfall | |

+. +

| | Adelaide | 1295 | 1158259 | 600.50 | |

| | Brisbane | 5905 | 1857594 | 1146.40 | |

| | Darwin | 112 | 120900 | 1714.70 | |

| | Hobart | 1357 | 205556 | 619.50 | |

+. +

+. +

| | City name | Area | Population | Annual Rainfall | |

+. +

| | Adelaide | 1295 | 1158259 | 600.50 | |

| | Brisbane | 5905 | 1857594 | 1146.40 | |

| | Darwin | 112 | 120900 | 1714.70 | |

| | Hobart | 1357 | 205556 | 619.50 | |

+. +

+-+

| | City name | Area | Population | Annual Rainfall | |

+-+

| | Adelaide | 1295 | 1158259 | 600.50 | |

| | Brisbane | 5905 | 1857594 | 1146.40 | |

| | Darwin | 112 | 120900 | 1714.70 | |

| | Hobart | 1357 | 205556 | 619.50 | |

+-+

+-+

| | City name | Area | Population | Annual Rainfall | |

+-+

| | Adelaide | 1295 | 1158259 | 600.50 | |

| | Brisbane | 5905 | 1857594 | 1146.40 | |

| | Darwin | 112 | 120900 | 1714.70 | |

| | Hobart | 1357 | 205556 | 619.50 | |

+-+

-

At this point, the study of "the usage of Python practical library PrettyTable" is over. I hope to be able to 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