Search code examples
python-2.7web-scrapingprettytable

How would I format data in a PrettyTable?


I'm getting the text from the title and href attributes from the HTML. The code runs fine and I'm able to import it all into a PrettyTable fine. The problem that I face now is that there are some titles that I believe are too large for one of the boxes in the table and thus distort the entire PrettyTable made. I've tried adjusting the hrules, vrules, and padding_width and have not found a resolution.

from bs4 import BeautifulSoup
from prettytable import PrettyTable
import urllib


r = urllib.urlopen('http://www.genome.jp/kegg-bin/show_pathway?map=hsa05215&show_description=show').read()
soup = BeautifulSoup((r), "lxml")


links = [area['href'] for area in soup.find_all('area', href=True)] 
titles = [area['title'] for area in soup.find_all('area', title=True)]


k = PrettyTable()
k.field_names = ["ID", "Active Compound", "Link"]
c = 1
for i in range(len(titles)):
    k.add_row([c, titles[i], links[i]])
    c += 1

print(k)

How I would like the entire table to display as:

print (k.get_string(start=0, end=25))

If PrettyTable can't do it. Are there any other recommended modules that could accomplish this?


Solution

  • This was not a formatting error, but rather the overall size of the table created was so large that the python window could not accommodate all the values on the screen.

    This proven by changing to a much smaller font size. If it helps anyone exporting as .csv then arranging in Excel helped.