We have now extracted the following form of list data.
[['A(S)''B(M)']
['1234'
'{ "voltage": {"name": {"S": "R00070"}, "button": {"L": [{"N":"29"}, {"N":"0"}}']
['5678'
'{ "voltage": {"name": {"S": "R00080"}, "button": {"L": [{"N":"30"}, {"N":"1"}}']
・・・
・・・
・・・
I would like to convert the above form into a simple list instead of multiple.
Button is exactly the same parameter, but only the number is different, so you need to separate the parameter names.
I would like you to tell me a good way to do it even if I look into various things.Thank you for your cooperation.
['A(S)'B(M)_voltage_name_s'B(M)_button_L_N'B(M)_button_L_N1']
['1234' 'R00070' '29' '0']
['1234' 'R00080' '30' '1']
・・・
・・・
・・・
I think I can apply the following article.
Python flatten multilevel JSON
Flattening JSON objects in Python
But before that, I will make some changes in the questionnaire.
}
, so I will supplement this as well.s
after synthesis is lowercase, but this should remain in uppercase S
.L
and N
and the number of columns should not change by 4.(R
and M
do not increase and columns do not decrease) The program is as follows:
##Sample Data Settings
## (Insert a comma in the delimiter of each data.
## It's all single-single quotation.
## Add } to the end of dictionary data)
data=\
['A(S)', 'B(M)'],
['1234',
{ 'voltage': {'name': {'S': 'R00070'},
'button': {'L': [{'N':'29'}, {'N':'0'}}},
['5678',
{ 'voltage': {'name': {'S': 'R00080'},
US>'button': {'L':[{'N':'30'}, {'N':'1'}}}]
]
## You might want to stop the whole processing of this part and have a fixed header line.
def flatten_key(h,d):## column name creation function
out = [h[0]]
base=h[1]
def flatten(x, name='):
if type(x)is dict:
For a in x:
flatten(x[a], name+'_'+a)
elif type(x)is list:
name = name.replace('_voltage',')## This line is forced (deleted '_voltage' for button name)
k = ' '
for a in x [0]:
k = a
for i in range (len(x)):
key=name+'_'+k+(str(i)if(i!=0)else')
out.append(base+key)
else:
out.append(base+name)
flatten(d[1])
return out
header=data.pop(0)## Extract original header row to create column name (removed from original data)
header=flatten_key(header,data[0])##Call column name creation function in original header row and first row of data
def flatten_value(d):## data row creation function
out = [d[0]]
def flatten(x):
if type(x)is dict:
For a in x:
flatten(x[a])
elif type(x)is list:
i = 0
For a in x:
flatten(a)
i+=1
else:
out.append(x)
flatten(d[1])
return out
body = [flatten_value(d) for data] ## Translating data portions with list inclusion
newdata=[header]## Concatenate column names with data to create new lists and display results
newdata.extend(body)
print(newdata)
Here's the result:
['A(S), 'B(M)_voltage_name_S', 'B(M)_button_L_N', 'B(M)_button_L_N1'], ['1234', 'R00070', '29', '0'], ['5678', 'R00080', '30', '1']]]]
If it looks good, it looks like the following.
['A(S), 'B(M)_voltage_name_S', 'B(M)_button_L_N', 'B(M)_button_L_N1'],
['1234', 'R00070', '29', '0'],
['5678', 'R00080', '30', '1']
© 2024 OneMinuteCode. All rights reserved.