insert = sql_table.insert() headers = t.headers() rows = [dict(zip(headers, row)) for row in t.to_rows()] for row in rows: c.execute(str(insert), row) conn.commit() else: print 'Already saved report %s' % report_data['detail_url'] c.execute('select date_filed from reports order by date_filed limit 1') oldest_year = parser.parse(c.fetchone()[0]).year c.execute( 'select date_filed from reports order by date_filed desc limit 1') newest_year = parser.parse(c.fetchone()[0]).year c.execute('select * from reports limit 1') header = list(map(lambda x: x[0], c.description)) for year in range(oldest_year, newest_year + 1): oldest_date = '%s-01-01' % year newest_date = '%s-12-31' % year c.execute( 'select * from reports where date_filed >= ? and date_filed <= ?', (oldest_date, newest_date)) rows = c.fetchall() outp = StringIO() writer = UnicodeCSVWriter(outp) writer.writerow(header) writer.writerows(rows) outp.seek(0) k.key = 'Reports/%s.csv' % year k.set_contents_from_file(outp) k.make_public()
def writeCSV(fpath, output): with open(fpath, 'wb') as f: writer = UnicodeCSVWriter(f) writer.writerows(output)