简体   繁体   中英

How do I convert the output of a JSON Dump to a format where I can access the individual elements, and convert it to a datframe?

I have the output of a JSON dump:

JSON 转储

in str format and I have converted it into list of lists, using the following code:

match=re.findall('\(.*?\)',file) #find the elements between the brackets ( and ) using 
regex,then store it as a list of lists
NewList= [[x] for x in iter(match)] 
  
print("The new lists of lists: ",NewList)

This gives the following output:

输出

But now, i can't access the individual elements of the list since the whole list is treated as str, NewList[0][0] gives the first list

输出在这里

how do I access the elements inside a list and store them in a pandas dataframe?

Solution:
This code snippet might solve your issue:

final_list_vals = []
NewList = [["(1086732,'edit','sysop',0,NULL,'infinity',1307)"], ["(1086732,'edit','sysop',0,NULL,'infinity',1307)"]] # This is just a sample input.
for lst in NewList:
    final_list_vals.append(eval(lst[0].replace('NULL', 'None')))

column_lst = ['col_1', 'col_2', 'col_3', 'col_4', 'col_5', 'col_6', 'col_7']
df = pd.DataFrame(final_list_vals, columns=column_lst)

NOTE: Add column list as per your requirement.

Leave a comment in case it does not solve your purpose.

OUTPUT:

     col_1 col_2  col_3  col_4 col_5     col_6  col_7
0  1086732  edit  sysop      0  None  infinity   1307
1  1086732  edit  sysop      0  None  infinity   1307

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM