Created
August 4, 2022 13:08
-
-
Save freemandealer/c2700009a9941c6af74f2b0041e8a1c9 to your computer and use it in GitHub Desktop.
flaten json file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python3 | |
import json | |
import sys | |
def data_flatten(key,val,con_s='_',basic_types=(str,int,float,bool,complex,bytes)): | |
""" | |
数据展开生成器,以键值对为最基础的数据 | |
param key: 键,默认为基础类型数据,不做进一步分析 | |
param val: 值,判断值的数据类型,如果为复杂类型就做进一步分析 | |
param con_s: 拼接符,当前级的键与父级键拼接的连接符,默认为_ | |
param basic_types: 基础数据类型元组,如果值的类型在元组之内,则可以输出 | |
return: 键值对元组 | |
""" | |
if isinstance(val, dict): | |
for ck,cv in val.items(): | |
yield from data_flatten(con_s.join([key,ck]).lstrip('_'), cv) | |
elif isinstance(val, (list,tuple,set)): | |
for item in val: | |
yield from data_flatten(key,item) | |
elif isinstance(val, basic_types) or val is None: | |
yield str(key).lower(),val | |
infile = open(sys.argv[1], 'r') | |
outfile = open(sys.argv[1] + '.flat.json', 'a+') | |
lines = infile.readlines() | |
for line in lines: | |
text = json.loads(line) | |
result_dict = {} | |
for i in data_flatten('',text): | |
result_dict[i[0]] = i[1] | |
json_str = json.dumps(result_dict) + "\n" | |
outfile.write(json_str) | |
infile.close() | |
outfile.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment