我编写了一个 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 的查询返回的内容,但是有没有办法格式化这些信息以便于阅读?
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
我过去这样做的方法是简单地使用 pandas 数据框。
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)
不使用外部库,在这里回答:
在不使用库的情况下以格式打印表格,SQLite 3 python
检查每一列,获取列宽,并用它来打印每一行。 或者只是检查每一行并打印它。