Search code examples
pythonprettytable

python 3 PrettyTable


How can I get my output in the table?

from prettytable import PrettyTable
import sqlite3

x = PrettyTable(["URL"])
x.padding_width = 1 

con = sqlite3.connect('C:\\Users\\joey\\AppData\\Local\\Google\\Chrome\\User Data\\Default\\History')
cursor = con.cursor()
d = cursor.execute("SELECT URL FROM 'urls' ;")
for data in d:
    x.add_row([data])


print(x)

How can I add the output of my code in the table? This code is not working. The code under this text is good. But how can I get my output in this and not the "test".

x = PrettyTable(["URL"])
x.padding_width = 1 

x.add_row(["test"])

Solution

  • The cursor returns tuples as you iterate over it. So instead of [data] (which would be a list containing a tuple), pass data itself to add_row:

    for data in cursor:
        x.add_row(data)