[Chicago] 2 unique keys in a python dict?

Lukasz Szybalski szybalski at gmail.com
Thu Oct 9 21:24:41 CEST 2008


On Thu, Oct 9, 2008 at 1:45 PM, Massimo Di Pierro
<mdipierro at cs.depaul.edu> wrote:
> In a more concise terms. Say I have a table "mytable" with a column a
> (double) and b (string) and want to read data in from a csv file, group by
> b, add all a's for the same b and write the result back in CSV. In web2py I
> do (complete code):
> db.define_table('mytable,SQLField('a','double'),SQLField('b'))
> db.mytable.import_from_csv_file(open('filename.csv'))
> print
> str(db().select(db.mytable.a.sum(),db.mytable.b,orderby=db.mytable.b,groupby=db.mytable.b))
> How do I do it in Django?
>
> Massimo
> On Oct 9, 2008, at 1:30 PM, James Snyder wrote:
>
> Someone has written a script that will do that:
> http://www.djangosnippets.org/snippets/788/
> You'd first have to create your model and do a syncdb to set up the tables,
> but that's easy
> I don't have a full code example, but ORDER BY is not hard with the Django
> ORM. Look for order_by in the query
> docs: http://docs.djangoproject.com/en/dev/topics/db/queries/
>
> Also, it may not give you nice object mapping goodness, but using the
> sqlite3 and csv modules aren't too bad.  Note that going that route,
> however, will require writing the SQL queries yourself :-)
> -jsnyder
> On Oct 9, 2008, at 1:05 PM, Massimo Di Pierro wrote:
>
> Can the Django ORM import CSV files and/or generate ORDER BY queries without
> additional code snippets?
> If so, could you post example?


I use sqlalchemy to read from sqlserver2000, and mysql.

I use csv module with a DictReader so I get a dictonary. Based on that
you I loop csv records and use sqlalchemy to get more information. If
you want you can create any table you want...which supports sqlite,
memory , mysql, m$sql, postgre, oracle,... but for this purpose its a
1 in 5 years report.

order by, group by etc is supported, as well as session, transaction,
autocommit....

One of the sql query looks like:
allrecords=session.query(Records).filter(sqlalchemy.and_(Records.CASE_NO==(CASE_NUMBER),Records.TYPE!='I').order_by(Records.CLAIM_NO).all()

Can't get easier than that.....put this in a loop, and for any record
you can get the information you want from database....

sqlalchemy and cvs module works for me.
Lucas


More information about the Chicago mailing list