繁体   English   中英

如何将 JSON 转储的输出转换为可以访问各个元素的格式,并将其转换为数据帧?

[英]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?

我有一个 JSON 转储的输出:

JSON 转储

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)

这给出了以下输出:

输出

但是现在,我无法访问列表的各个元素,因为整个列表都被视为 str,NewList[0][0] 给出了第一个列表

输出在这里

如何访问列表中的元素并将它们存储在 Pandas 数据框中?

解决方案:
此代码片段可能会解决您的问题:

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)

注意:根据您的要求添加列列表。

如果它不能解决您的目的,请发表评论。

输出:

     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.

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