Mercurial > hg > cc > cirrus_home
annotate bin/cdx2tsv.py @ 121:863ea87be6bb
support field edit
author | Henry S. Thompson <ht@inf.ed.ac.uk> |
---|---|
date | Mon, 28 Jun 2021 15:40:10 +0000 |
parents | d0b544e53dda |
children | a76cc0df2754 |
rev | line source |
---|---|
120
d0b544e53dda
for use in processing CC index files
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
121 | 2 '''Extract named fields, with optional post-processing, in order, |
3 from a Common Crawl index row | |
4 | |
5 Field specs on command line are either an atom or | |
6 a tuple of atom and expression with | |
7 free variable f | |
8 For example | |
9 > cdx2tsv.py mime '(url,f.split(":",maxsplit=1)[0])' < xyzzy.cdx | |
10 will output media type and URI scheme''' | |
120
d0b544e53dda
for use in processing CC index files
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
11 import json,sys |
d0b544e53dda
for use in processing CC index files
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
12 |
d0b544e53dda
for use in processing CC index files
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
13 fields=sys.argv[1:] |
d0b544e53dda
for use in processing CC index files
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
14 |
121 | 15 fields=[((lambda x,y:(x,eval("lambda f:%s"%y)))(*(f[1:-1].split(',',maxsplit=1))) if f[0]=='(' else f) for f in fields] |
120
d0b544e53dda
for use in processing CC index files
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
16 for l in sys.stdin: |
d0b544e53dda
for use in processing CC index files
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
17 (key,stamp,jj)=l.rstrip().split(' ',maxsplit=2) |
d0b544e53dda
for use in processing CC index files
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
18 ja=json.loads(jj) |
121 | 19 print('\t'.join((ja.get(f,'NA') if isinstance(f,str) else |
20 ((f[1](ja[f[0]]) if f[0] in ja else 'NA'))) for f in (ja.keys() if fields==["*"] else fields))) | |
21 |