在python中拆分json数据

时间:2013-09-24 17:39:14

标签: python json

我正在尝试操作python中的项列表但是我得到错误“AttributeError:'list'对象没有属性'split'” 我知道列表不理解.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)

要了解我正在尝试做什么,您还需要查看json数据。 Ting输出这个:

[
  [1379955600000L, 123.107310846774], [1379959200000L, 124.092526428571],
  [1379962800000L, 125.539504822835], [1379966400000L, 126.27024617931],
  [1379970000000L, 126.723474983766], [1379973600000L, 126.242406356837],
  [1379977200000L, 124.788410570987], [1379980800000L, 126.810084904632],
  [1379984400000L, 128.270580796748], [1379988000000L, 127.892411269036],
  [1379991600000L, 126.140579640523], [1379995200000L, 126.513705084746],
  [1379998800000L, 128.695124951923], [1380002400000L, 128.709738051044],
  [1380006000000L, 125.987767097378], [1380009600000L, 124.323433535528],
  [1380013200000L, 123.359378559603], [1380016800000L, 125.963250678733],
  [1380020400000L, 125.074618194444], [1380024000000L, 124.656345088853],
  [1380027600000L, 122.411303435449], [1380031200000L, 124.145747100372],
  [1380034800000L, 124.359452274881], [1380038400000L, 122.815357211394],
  [1380042000000L, 123.057706915888]
]

[
  [1379955600000L, 536.4739135], [1379959200000L, 1235.42506637],
  [1379962800000L, 763.16329656], [1379966400000L, 804.04579319],
  [1379970000000L, 634.84689741], [1379973600000L, 753.52716718], 
  [1379977200000L, 506.90632968], [1379980800000L, 494.473732950001],
  [1379984400000L, 437.02095093], [1379988000000L, 176.25405034],
  [1379991600000L, 319.80432715], [1379995200000L, 206.87212398],
  [1379998800000L, 638.47226435], [1380002400000L, 438.18036666],
  [1380006000000L, 512.68490443], [1380009600000L, 904.603705539997],
  [1380013200000L, 491.408088450001], [1380016800000L, 670.275397960001],
  [1380020400000L, 767.166941339999], [1380024000000L, 899.976089609997], 
  [1380027600000L, 1243.64963909], [1380031200000L, 1508.82429811],
  [1380034800000L, 1190.18854705], [1380038400000L, 546.504592349999], 
  [1380042000000L, 206.84883264]
]

并且[0]输出:

[1379955600000L, 123.187067936508]
[1379955600000L, 536.794013499999]

我真正想做的是将第二个逗号后的ting [0-24]中的值加起来。这让我试图分裂,但这不起作用

非常感谢帮助!

2 个答案:

答案 0 :(得分:2)

你已经拥有一个列表;这些逗号由Python放在那里,仅在打印列表时分隔值。

直接访问元素2

print ting[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]

答案 1 :(得分:1)

当您使用json.loads加载数据时,它已被解析为可以正常切片和索引的实际列表。如果您希望数据以第三个元素开头,请使用ting[2:]。 (如果您只想要第三个元素,只需使用ting[2]。)