简体   繁体   中英

Output pretty table to an html file?

I can't seem to get a clear cut answer on how to do this. I have a pretty table that I print to the terminal but what if I want to create an html file and print the table to that html file? How can this be done.

This is my table that I have, how could I go about writing this to a new html file?

tbl = PrettyTable(["Occurs", "Occurrences"])
for word, cnt in matches.items():
    tbl.add_row([word, cnt])
tbl.align = 'l'
print(tbl.get_string(sortby="Occurrences", reversesort=True))

You have the get_html_string ( doc on the github readme ) which will allow you to have the html and then you write the string into a html file like this:

from pathlib import Path
tbl = PrettyTable(["Occurs", "Occurrences"])
for word, cnt in matches.items():
    tbl.add_row([word, cnt])
tbl.align = 'l'
to_save = tbl.get_html_string(sortby="Occurrences", reversesort=True)
with Path("my_html_file.html").open(mode="w", encoding="utf-8") as fp:
    fp.write(to_save)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM