Aggregate in SQLite or Python

2019-08-05 06:40发布

Suppose I have a table with a article headers, author, and also a publication date, I retrieve all of the ones that are published after July 1st, - but now I want to iterate through not the article headers individually, but through sets of all the articles published on each day, what's the best and most pythonic way to build this list. Can it be done in the sqlite query?

Edit: I don't actually have a table with articles in sqlite3, but let's suppose I did. And suppose the table articles is organized with:

title TEXT, author TEXT, publisher TEXT, date DATETIME

The articles might be fetched like so:

cursor.execute("SELECT * FROM articles where date > ?", \ 
(datetime.datetime(2014, 07, 01),))

and could be grouped by (Following Holdenweb's answer below):

itertools.groupby(cursor.fetchall(), lambda x: datetime.strptime(x[3], '%Y-%m-%d %H:%M:%S.%f').day)

which will give a tuple of (day, group), and can be iterated over in the manner described below.

1条回答
相关推荐>>
2楼-- · 2019-08-05 07:23

A SQL query will normally only return a single set of rows as a result.

Let's say you have retrieved all the rows you want in a cursor curs, and assume that the resulting rows are each made up of (header, author, pub_date) and that you sorted the data in SQL in ascending order of pub_date.

Using itertools.groupby() it's very easy to define a key function that returns the pub_date colum (lambda r: r[2] comes to mind). You can then iterate over the groupby() result which is a series of (key_value, group) tuples where key_value will take the successive unique values of the pub_date column and group will be an iterator yielding successive rows associated with that key value. Try something like

for key_val, group in itertools.groupby(curs.fetchall(), lambda r: r[2]):
    print key_val
    for item in group:
        print "\t", item

to verify this works as you want.

查看更多
登录 后发表回答