列表索引必须是整数或切片而不是str

时间:2019-05-29 15:40:23

标签: python pandas

我有这样的代码

def myfunction(json_dict):
     mylistsize = len((list(json_normalize(json_dict,'data')['title'])))-1
     print(mylistsize)
     data = [c for c in json_dict['data']][0]#This line when it is 0 or 1 or 2 works for me.
     df = pd.DataFrame()
     data_paragraphs = data['paragraphs']

但是当我更改它以包括数组的所有元素时,就变成这样-

data = [c for c in json_dict['data']][0:mylistsize]

这给我这行错误-data_paragraphs = data['paragraphs']

请帮助我。

1 个答案:

答案 0 :(得分:2)

datadata = [c for c in json_dict['data']][0:mylistsize]中的列表。您可以像data[0]这样访问其中的值。要访问列表中某项的json ,请执行data[0]['paragraphs']