程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python beautifies output tables with prettytable built-in Library

編輯:Python

Catalog

Preface :

install

Case study

from csv File add data , And print out the form

from HTML Import data

Preface :

Most of the time , The information that needs to be output can be output neatly , In the use of mysql When , After we use the command character , Will output a particularly good-looking table ,python Of prettytable Library is such a tool , It can help us print out good-looking forms , And especially friendly to Chinese support

install

prettytable yes pyhton Built in Library , You can install... Directly through the command

pip install prettytable Case study from prettytable import PrettyTabletable = PrettyTable([' full name ', 'ID', 'Salary'])table.add_row(['Phil', '0001', '10000'])table.add_row(['Joge', '0002', '30000'])print(table)

Add data by row table.add_row()
You can also add data by column table.add_column()

There will be some differences here :

First use **PrettyTable()** Create a table ,add_column(x,[]),x Is the column name ,[] The following list is the value of each column

import sysfrom prettytable import PrettyTabletable = PrettyTable()table.add_column(' full name ', ['Phil', 'Joge'])table.add_column('ID',['0002', '0001'])print(table)

from csv File add data , And print out the form

at present prettytable Support csv, I won't support it xlsx

from prettytable import PrettyTablefrom prettytable import from_csvtable = PrettyTable()file = open('test.csv','r')table = from_csv(file)print(table)file.close()

from HTML Import data from prettytable import PrettyTablefrom prettytable import from_htmlhtml = '''<table><str><tr><th> full name </th><th>ID</th></tr><tr><td>Vergil</td><td>001</td></tr><tr><td>Dante</td><td>002</td></tr></table>'''table = from_html(html)print(table)

And support sql Input , I'm not going to do that here
prettytable It also supports custom table styles 、 Table slice 、 Output the specified line and other functions

Custom form... Here :

from prettytable import PrettyTablefrom prettytable import from_csvtable = PrettyTable()file = open('test.csv','r')table = from_csv(file)table.border = Truetable.junction_char = '%'table.horizontal_char = '+'table.vertical_char = '^'print(table)file.close()

This is about python Use prettytable This is the end of the article on beautifying output tables with built-in Libraries , More about python Please search the previous articles of SDN or continue to browse the related articles below. I hope you will support SDN more in the future !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved