[英]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: str 格式,我已将其转换为列表列表,使用以下代码:
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但是现在,我无法访问列表的各个元素,因为整个列表都被视为 str,NewList[0][0] 给出了第一个列表
how do I access the elements inside a list and store them in a pandas dataframe?如何访问列表中的元素并将它们存储在 Pandas 数据框中?
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.