Wednesday, April 25, 2012

Python split file based on json tag


'''A script to split places_dump_US.geojson into small files based on the json tag, province'''
'''script start'''


import json

def writeState(m,state='none'):
    out_name='places_dump_US.'+state+'.geojson';
    tg=open(out_name,'a',-1)
    print(json.dumps(m),file=tg)
    tg.close

sf=open('places_dump_US.geojson','r',-1)
count=0;
for line in sf:
    try:
        p=json.loads(line)
        cstate = p.get('properties').get('province')
        writeState(p,cstate)
        print(count)
        count+=1
    except:
        writeState(p)
        print(count)
        count+=1
sf.close


'''script end'''

No comments:

Post a Comment