[英]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 轉儲的輸出:
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.