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.
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 thegroupby()
result which is a series of(key_value, group)
tuples wherekey_value
will take the successive unique values of thepub_date
column andgroup
will be an iterator yielding successive rows associated with that key value. Try something liketo verify this works as you want.