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
installprettytable 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 !