2013-09-24 113 views
0

我想操縱python中的項目列表,但我得到的錯誤「AttributeError:'列表'對象沒有屬性'分裂'」 我明白,列表不明白.split我不知道還有什麼要做。以下是我的代碼相關部分的複製粘貼。python分裂json數據

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數據。婷輸出這樣的:

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

我真正想要做的是從婷加起來值[0-24]自帶的後第二個逗號。這讓我試圖做一個拆分,但這不起作用

幫助表示讚賞!

+0

您的所有評論都讓我意識到正是部分我是越來越錯誤:數據被媒體鏈接列出,僅輸出逗號什麼時候打印。非常感謝你的幫助。獎金問題:我如何以聰明的方式添加tola [1] [u'values'] [0] [1] + tola [1] [u'values'] [1] [1]? – user2791993

回答

1

你已經一個列表;只有在打印列表時,逗號纔會被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] 
+0

實際上婷[2]輸出 [1379962800000L,125.539504822835] [1379962800000L,763.16329656] 和IM試圖將763的值添加到其餘 – user2791993

+0

@ user2791993 :我使用示例代碼中的URL測試了它。 –

+0

@ user2791993:啊,你是從'tola'中的每個子列表打印參考2,所以'tola [0] ['values'] [2]'和'tola [1] ['values'] [2]',等等。 –

1

當您使用json.loads加載數據時,它已被解析爲真正的列表,您可以像平常一樣切片和編制索引。如果你想要從第三個元素開始的數據,只需使用ting[2:]。 (如果你只是想要第三個元素本身,只需使用ting[2]。)

+0

我並不是試圖訪問ting [2],而是訪問ting [2]中的一個特定值,這是我的失敗。汀[2]的輸出[1379962800000L,125.539504822835] [1379962800000L,763.16329656] – user2791993