在Python中打印格式正确的SQLite表

时间:2016-05-05 13:10:13

标签: python sqlite

我编写了一个Python脚本来向表中添加行。我决定如果我还可以使用相同的脚本查看我的表而不必退出脚本并运行sqlite3或切换到另一个shell并运行sqlite3,那将是很好的。所以我写了我期望的东西会给我我想要的东西而且它有点......这是有问题的剧本的一部分:

import sqlite3

conn = sqlite3.connect('stu.db')
c = conn.cursor()

var = 1
while var == 1:

    enquiry = raw_input("What would you like to do?> ")

    enquiry == 'stu db' or enquiry == 'sd':
    c.execute("SELECT * FROM stu")
    conn.commit

在sqlite3中,当您运行SELECT * FROM stu时,您将获得一个格式良好的表,其中包含统一的行和列。当我在这里运行时,我会在括号中获得一长串信息。它看起来有点像这样(我没有打印实际结果,因为这违反了一些联邦法律):

[(1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None), (1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),(1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),(1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),(1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),(1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),(1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),(1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),(1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),(1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None)]

我想我知道发生了什么事。 Python只是吐出sqlite返回的查询,但有没有办法格式化这些信息,以便它易于阅读?

2 个答案:

答案 0 :(得分:4)

您可以使用pandas

print pd.read_sql_query("SELECT * FROM stu", conn)

示例程序(python 2.7.6,pandas 0.18.0):

import sqlite3
import pandas as pd

conn = sqlite3.connect(':memory:')
c = conn.cursor()

c.execute('create table stu ( ID, Name, ShoeSize, Course, IQ, Partner )')
conn.commit()
c.executemany('insert into stu VALUES (?, ?, ?, ?, ?, ?)',
    [(1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),
     (1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),
     (1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),
     (1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),
     (1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),
     (1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),
     (1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),
     (1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),
     (1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),
     (1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None)])
conn.commit()


# Ugly way
print list(c.execute("SELECT * FROM stu"))

# Pretty way
print pd.read_sql_query("SELECT * FROM stu", conn)

结果,包括丑陋和漂亮的输出:

[(1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None), (1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None), (1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None), (1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None), (1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None), (1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None), (1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None), (1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None), (1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None), (1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None)]
           ID      Name  ShoeSize   Course  IQ Partner
0  1234567890  John Doe      3852  DEGR-AA   4    None
1  1234567890  John Doe      3852  DEGR-AA   4    None
2  1234567890  John Doe      3852  DEGR-AA   4    None
3  1234567890  John Doe      3852  DEGR-AA   4    None
4  1234567890  John Doe      3852  DEGR-AA   4    None
5  1234567890  John Doe      3852  DEGR-AA   4    None
6  1234567890  John Doe      3852  DEGR-AA   4    None
7  1234567890  John Doe      3852  DEGR-AA   4    None
8  1234567890  John Doe      3852  DEGR-AA   4    None
9  1234567890  John Doe      3852  DEGR-AA   4    None

答案 1 :(得分:0)

我过去这样做的方法就是简单地使用熊猫数据框。

import pandas as pd

data = [(1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None), (1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),(1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),(1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),(1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),(1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),(1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),(1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),(1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None),(1234567890, u'John Doe', 3852, u'DEGR-AA', 4, None)]

pd.DataFrame(data)