LoginSignup
3
2

More than 5 years have passed since last update.

pythonでjsonをtsvに

Posted at
import glob
import json

def listArray(key,arr):
    for a in arr:
        if type(a) is list:
            listArray(key, a)
        elif type(a) is dict:
            listDic(key, a)
        else:
            print(key, a)


def listDic(key,dic):
    for k,v in dic.items():
        if type(v) is list:
            listArray(key+"\t"+k, v)
        elif type(v) is dict:
            listDic(key+"\t"+k, v)
        else:
            print(key+"\t"+k, v)

for fpath in glob.glob("./*.json"):

    print(fpath)
    f=open(fpath, 'r')

    dic=json.load(f)

    for key,val in dic.items():

        if type(val) is list:
            listArray(key, val)
        elif type(val) is dict:
            listDic(key, val)
        else:
            print(key, val)

keyはスタックしていきます

3
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2