在python中拆分json数据

在python中拆分json数据,python,json,Python,Json,我试图在python中操作一个项目列表,但得到了错误“AttributeError:'list'对象没有属性“split” 我知道那个名单不明白,但我不知道还能做什么。下面是我代码相关部分的复制粘贴 tourl = 'http://data.bitcoinity.org/chart_data' tovalues = {'timespan':'24h','resolution':'hour','currency':'USD','exchange':'all','mining_pool':'all'

我试图在python中操作一个项目列表,但得到了错误“AttributeError:'list'对象没有属性“split” 我知道那个名单不明白,但我不知道还能做什么。下面是我代码相关部分的复制粘贴

tourl = 'http://data.bitcoinity.org/chart_data'
tovalues = {'timespan':'24h','resolution':'hour','currency':'USD','exchange':'all','mining_pool':'all','compare':'no','data_type':'price_volume','chart_type':'line_bar','smoothing':'linear','chart_types':'ccacdfcdaa'}
todata = urllib.urlencode(tovalues)
toreq = urllib2.Request(tourl, todata)
tores = urllib2.urlopen(toreq)
tores2 = tores.read()
tos = json.loads(tores2)
tola = tos["data"]
for item in tola:
    ting = item.get("values")
    ting.split(',')[2]       <-----ERROR
    print(ting)
而ting[0]则输出:

[1379955600000L,123.187067936508]
[1379955600000L,536.79401349999]

我真正想做的是把第二个逗号后面的ting[0-24]的值相加。这使我尝试进行拆分,但当您使用
json.loads加载数据时,该拆分不起作用,它已经被解析为一个真正的列表,您可以像正常情况一样进行切片和索引。如果您希望数据以第三个元素开始,只需使用
ting[2:]
。(如果您只需要第三个元素本身,只需使用
ting[2]

当您使用
json.loads
加载数据时,它已经被解析为一个真实的列表,您可以像平常一样进行切片和索引。如果您希望数据以第三个元素开始,只需使用
ting[2:]
。(如果您只需要第三个元素本身,只需使用
ting[2]

您已经有了一个列表;Python将逗号放在那里,以仅在打印列表时分隔值

只需直接访问元素2:

这张照片是:

[1379962800000, 125.539504822835]
item['values']
(因此
ting
)中的每个条目都是两个浮点值的列表,因此您可以使用索引0和1来处理每个浮点值:

>>> print ting[2][0]
1379962800000
>>> print ting[2][1]
125.539504822835
要获得所有第二个值的列表,可以使用列表:

second_vals = [t[1] for t in ting]
您已经有了一个列表;Python将逗号放在那里,以仅在打印列表时分隔值

只需直接访问元素2:

这张照片是:

[1379962800000, 125.539504822835]
item['values']
(因此
ting
)中的每个条目都是两个浮点值的列表,因此您可以使用索引0和1来处理每个浮点值:

>>> print ting[2][0]
1379962800000
>>> print ting[2][1]
125.539504822835
要获得所有第二个值的列表,可以使用列表:

second_vals = [t[1] for t in ting]

实际上,ting[2]输出[1379962800000L,125.539504822835][1379962800000L,763.16329656],我正在尝试将763值添加到rest@user2791993:我使用示例代码中的URL对此进行了测试。@user2791993:Ah,您正在打印
tola
中每个子列表中的引用2,因此
tola[0]['values'][2]
tola[1]['values'][2]
等。Tour='tovalues={'timespan':'24h','resolution':'hour','currency':'USD','exchange':'all','mining_pool':'all','compare':'no','data_type':'price_volume','chart_type':'line(bar','smoothing':'line线性','chart(types':'CCACCDFCDAA'}todata=urllib.urlencode(TorValues)toreq=urllib2请求(tourl,todata)tores=urllib2.urlopen(toreq)tores2=tores.read()tos=json.loads(tores2)tola=tos[“data”]用于tola:ting=item.get(“values”)print(ting[2])@user2791993中的项:您需要明确需要在这里配对哪些值。实际上,ting[2]输出[1379962800000L,125.539504822835][1379962800000L,763.16329656]我正在尝试将763值添加到rest@user2791993:我使用示例代码中的URL对此进行了测试。@user2791993:Ah,您正在打印
tola
中每个子列表中的引用2,因此
tola[0]['values'][2]
tola[1]['values'][2]
,等等。Tour=''tovalues={'timespan':'24h','resolution':'hour','currency':'USD','exchange':'all','mining_pool':'all','compare':'no','data_type':'price_volume','chart_type':'line_bar','smoothing':'linear','chart_types':'CCACDFCDFCDAA}todata=urllib.urlencode(tovalues)toreq=urllib2请求(Tour,todata)tores=urllib2.urlopen(toreq)tores2=tores.read()tos=json.loads(tores2)tola=tos[“数据”]用于tola:ting=item.get(“值”)print(ting[2])@user2791993:您需要弄清楚需要在这里配对哪些值。我不想只访问ting[2]但是ting[2]中的一个特定值,即im失败。ting[2]输出[137996280000L,125.539504822835][137996280000L,763.16329656]我不想只访问ting[2],而是ting[2]中的一个特定值,即im失败。ting[2]输出[137996280000L,125.539504822835][137996280000L,763.16329656]你的所有评论让我意识到我犯了什么错误:数据已全部列出,打印时只输出逗号等。非常感谢你的帮助。附加问题:我如何将tola[1][u'values'][0][1]+tola[1][u'values'][1][1]相加以一种聪明的方式?你所有的评论让我意识到我犯了什么错误:数据已全部列出,打印时只输出逗号等等。非常感谢你的帮助。附加问题:我如何以一种聪明的方式将tola[1][u'values'][0][1]+tola[1][u'values'][1]相加?