I have the output of a JSON dump:
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.