Demo entry 6344089

koa

   

Submitted by zoro on Jan 11, 2017 at 10:17
Language: Python. Code size: 619 Bytes.

#!/usr/bin/env python
import json


def tab2json(infile, outfile=None):
    outfile = outfile or infile + '.json'
    with open(infile) as f, open(outfile, 'w') as out:
        headerlist = f.readline().strip().split('\t')
        dic = {'header': [], 'body': []}
        dic['header'] = headerlist
        for line in f:
            linelist = line.strip().split('\t')
            dic['body'].append(linelist)
        out.write(json.dumps(dic))


if __name__ == '__main__':
    import sys
    if len(sys.argv) < 2:
        print "python %s <infile> [outfile]" % sys.argv[0]
        exit(1)
    tab2json(*sys.argv[1:])

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).