Created
August 16, 2015 00:09
-
-
Save ketralnis/8124b3ead9d94e00dcb2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
try: | |
import simplejson as json | |
except ImportError: | |
import json | |
import tempfile | |
import sqlite3 | |
import subprocess | |
import sys | |
import os | |
def dumps(doc): | |
return json.dumps(doc, | |
separators=(',',':'), ##compact the output | |
indent=None, ##disable whitespace | |
check_circular=False, ##we know these aren't circular | |
) | |
conn = sqlite3.connect(':memory:') | |
created = False | |
with conn: | |
# all one transaction | |
for line in sys.stdin: | |
line = line.strip() | |
if not line: | |
continue | |
fields = line.split('\t') | |
fields = map(json.loads, fields) | |
if not created: | |
conn.execute("CREATE TABLE tbl(%s)" | |
% ','.join(('f%d'%x) for x in range(len(fields)))) | |
created = True | |
conn.execute("INSERT INTO tbl VALUES(%s);" % ','.join(('?',) * len(fields)), | |
fields) | |
for arg in sys.argv[1:]: | |
for line in conn.execute(arg): | |
fields = map(dumps, line) | |
print '\t'.join(fields) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
P.S.: The
tempfile
andsubprocess
modules are imported but unused.