Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fixes

  • Loading branch information...
commit c3eda35f943b9cccba11318e314c5244ff843ef6 1 parent 9d55786
@brendano authored
Showing with 40 additions and 12 deletions.
  1. +39 −10 json2tsv
  2. +1 −2  tsvcat
View
49 json2tsv
@@ -1,17 +1,33 @@
-#!/usr/bin/env python2.6
-from __future__ import print_function
+#!/usr/bin/env python
+#from __future__ import print_function
import simplejson
#import json as simplejson # py 3.0
-import sys,re
+import sys,re,itertools
import tsvutil
tsvutil.fix_stdio()
-json = simplejson.load(sys.stdin)
+#all_json = simplejson.load(sys.stdin)
+#assert isinstance(all_json, list) and len(all_json)>0
+#item1 = json[0]
+#keys = items1.keys()
+#keys.sort()
+#json_iter = all_json
-assert len(json)>0
-item1 = json[0]
-keys = item1.keys()
-keys.sort()
+def safe_json_iter(raw_json_iter):
+ for raw in raw_json_iter:
+ try:
+ json = simplejson.loads(raw)
+ except Exception, e:
+ print>>sys.stderr, type(e), e
+ yield json
+
+json_iter = safe_json_iter(sys.stdin)
+keys = sys.argv[1:]
+if not keys:
+ item1 = json_iter.next()
+ keys = item1.keys()
+ keys.sort()
+ json_iter = itertools.chain([item1], json_iter)
BAD = re.compile("[\r\n\t]")
@@ -19,6 +35,19 @@ def clean_cell(x):
if x is None: return ""
return BAD.sub(" ", unicode(x))
-for row in json:
- print(*[clean_cell(row[k]) for k in keys], sep="\t")
+def lookup(json, k):
+ # return json[k]
+ if '.' in k:
+ # jpath path
+ ks = k.split('.')
+ v = json
+ for k in ks: v = v[k]
+ return v
+ return json[k]
+
+print "\t".join(keys)
+for json in json_iter:
+ print "\t".join([clean_cell(lookup(json,k)) for k in keys])
+ #print "\t".join([clean_cell(json[k]) for k in keys])
+ #print(*[clean_cell(json[k]) for k in keys], sep="\t")
View
3  tsvcat
@@ -57,8 +57,7 @@ print "\t".join(all_cols)
for i,f in enumerate(files):
cols = file_cols[i]
for line in f:
- line = unicode(line,'utf8')
- #print repr(line)
+ #line = unicode(line,'utf8')
parts = line[:-1].split("\t")
hash = {}
for j in range(len(cols)):
Please sign in to comment.
Something went wrong with that request. Please try again.