I Have data structure like the one mentioned below which us all nested in terms of dictionaries :
{
'list':{
'5':{
'set_profile_type':'static',
'status_name_now':'OFF',
'name':'BTテザリング',
'status_code_now':'OFF',
'status_name_init':'OFF',
'status_list':{
'0':{},
'1':{}
},
'guide':'some utf text',
'choice_type':'select',
'get_profile':'BTtethering_GetOnOff',
'status_key_name':'onOff'
},
'4':{
'set_profile_type':'static',
'status_name_now':'OFF',
'name':'WiFiテザリング',
'status_code_now':'OFF',
'status_name_init':'OFF',
'status_list':{
'0':{},
'1':{}
},
'guide':'UTF text',
'choice_type':'select',
'get_profile':'PortableHotspot_GetOnOff',
'status_key_name':'onOff'
},
'2':{},
'0':{},
'6':{},
'1':{},
'3':{}
},
'name':'somePropert1'
}
当我试图将这个结构变成csv时,我用json.loads尝试了pandas库,上面的字符串是由于我使用了unserialize的ohpserialization库而形成的;是否有一个很好的和干净的方式来展平这个数据结构我也尝试过json_normalize但是这不能识别上面的字符串为json-
def deserialize(data):
import phpserialize
import ast
import json
data = "'"+data+"'"
data = ast.literal_eval(format(data)).encode('utf-8')
j1 = phpserialize.unserialize(data)
j1 = phpserialize.loads(phpserialize.dumps(j1), decode_strings=True)
j1 = json.dumps(j1)
return j1
def CustomParser(data):
import json
j1 = json.loads(data)
j1 = j1
return j1
df = pandas.read_csv(input_file,sep="|",converters=
{'device_udate':deserialize},header=0)
print (df)
df.to_csv(output_file,sep=",",encoding="utf-8")
df1 = pandas.read_csv(output_file,converters=
{'device_udate':CustomParser},header=0)
df1[sorted(df1['device_udate'][0].keys())] =
df1['device_udate'].apply(pandas.Series)
print (df1)