2017-09-05 60 views
-1

我下載了一個字符串從API爲這樣:Python的隱蔽部分字典

{"retCode":1,"retMsg":"Success","data":[{"secID":"000002.XSHE","ticker":"000002","exchangeCD":"XSHE","ListSectorCD":1,"ListSector":"主板","transCurrCD":"CNY","secShortName":"萬科A","secFullName":"萬科企業股份有限公司","listStatusCD":"L","listDate":"1991-01-29","equTypeCD":"A","equType":"滬深A股","exCountryCD":"CHN","partyID":3,"totalShares":11039152000,"nonrestFloatShares":11024120600,"nonrestfloatA":9709165100,"officeAddr":"廣東省深圳市鹽田區大梅沙環梅路33號萬科中心","primeOperating":"房地產業務及投資零售業務。","endDate":"2017-06-30","TShEquity":161157756356.52}]} 

,使整個事情是一個字符串,但我需要的是data後的東西「,它看起來像一本字典。

我如何才能得到字符串的一部分,並將其轉換爲實際的字典

回答

2

U可以使用json.loads從Python標準libary:

import json 
s = '{"retCode":1,"retMsg":"Success","data":[{"secID":"000002.XSHE","ticker":"000002","exchangeCD":"XSHE","ListSectorCD":1,"ListSector":"主板","transCurrCD":"CNY","secShortName":"萬科A","secFullName":"萬科企業股份有限公司","listStatusCD":"L","listDate":"1991-01-29","equTypeCD":"A","equType":"滬深A股","exCountryCD":"CHN","partyID":3,"totalShares":11039152000,"nonrestFloatShares":11024120600,"nonrestfloatA":9709165100,"officeAddr":"廣東省深圳市鹽田區大梅沙環梅路33號萬科中心","primeOperating":"房地產業務及投資零售業務。","endDate":"2017-06-30","TShEquity":161157756356.52}]}' 
print(json.loads(s)['data']) 

可選的是增加逃生(沒有必要在這種情況下):

json_acceptable_string = s.replace("'", "\"") 

返回

{'retCode': 1, 'data': [{'TShEquity': 161157756356.52, 'primeOperating': '房地產業務及投資零售業務。', 'secShortName': '萬科A', 'equType': '滬深A股', 'exchangeCD': 'XSHE', 'exCountryCD': 'CHN', 'listStatusCD': 'L', 'nonrestFloatShares': 11024120600, 'transCurrCD': 'CNY', 'totalShares': 11039152000, 'secID': '000002.XSHE', 'listDate': '1991-01-29', 'ListSectorCD': 1, 'endDate': '2017-06-30', 'nonrestfloatA': 9709165100, 'ListSector': '主板', 'equTypeCD': 'A', 'ticker': '000002', 'officeAddr': '廣東省深圳市鹽田區大梅沙環梅路33號萬科中心', 'secFullName': '萬科企業股份有限公司', 'partyID': 3}], 'retMsg': 'Success'} 

如果妳需要從字典中提取結束日期的值,則使用:

json.loads(s)['data'][0]['endDate'] 

Convert a String representation of a Dictionary to a dictionary?

+0

我試過了,但我需要secID,ticker..etc快譯通,json.loads()只給出了數據字典如你所說。 – Liam

1

Python有用於此目的的json模塊

import json 
s = json.loads('{"retCode":1,"retMsg":"Success","data":[{"secID":"000002.XSHE","ticker":"000002","exchangeCD":"XSHE","ListSectorCD":1,"ListSector":"主板","transCurrCD":"CNY","secShortName":"萬科A","secFullName":"萬科企業股份有限公司","listStatusCD":"L","listDate":"1991-01-29","equTypeCD":"A","equType":"滬深A股","exCountryCD":"CHN","partyID":3,"totalShares":11039152000,"nonrestFloatShares":11024120600,"nonrestfloatA":9709165100,"officeAddr":"廣東省深圳市鹽田區大梅沙環梅路33號萬科中心","primeOperating":"房地產業務及投資零售業務。","endDate":"2017-06-30","TShEquity":161157756356.52}]}') 
print(s["data"]) 

在Python 2將打印

[{u'primeOperating': u'\u623f\u5730\u4ea7\u4e1a\u52a1\u53ca\u6295\u8d44\u96f6\u552e\u4e1a\u52a1\u3002', u'ListSectorCD': 1, u'exchangeCD': u'XSHE', u'secID': u'000002.XSHE', u'secFullName': u'\u4e07\u79d1\u4f01\u4e1a\u80a1\u4efd\u6709\u9650\u516c\u53f8', u'nonrestFloatShares': 11024120600, u'endDate': u'2017-06-30', u'officeAddr': u'\u5e7f\u4e1c\u7701\u6df1\u5733\u5e02\u76d0\u7530\u533a\u5927\u6885\u6c99\u73af\u6885\u8def33\u53f7\u4e07\u79d1\u4e2d\u5fc3', u'listDate': u'1991-01-29', u'secShortName': u'\u4e07\u79d1A', u'TShEquity': 161157756356.52, u'equType': u'\u6caa\u6df1A\u80a1', u'nonrestfloatA': 9709165100, u'listStatusCD': u'L', u'ListSector': u'\u4e3b\u677f', u'partyID': 3, u'totalShares': 11039152000, u'transCurrCD': u'CNY', u'exCountryCD': u'CHN', u'ticker': u'000002', u'equTypeCD': u'A'}] 

而且在Python 3支持Unicode開箱,將打印

[{'nonrestfloatA': 9709165100, 'listDate': '1991-01-29', 'officeAddr': '廣東省深圳市鹽田區大 梅沙環梅路33號萬科中心', 'ListSectorCD': 1, 'secShortName': '萬科A', 'primeOperating': '房地產業務及投資零售業務。', 'secFullName': '萬科企業股份有限公司', 'TShEquity': 161157756356.52, 'exCountryCD': 'CHN', 'nonrestFloatShares': 11024120600, 'exchangeCD': 'XSHE', 'equType': '滬深A 股', 'secID': '000002.XSHE', 'ticker': '000002', 'endDate': '2017-06-30', 'transCurrCD': 'CNY', 'ListSector': '主板', 'equTypeCD': 'A', 'partyID': 3, 'listStatusCD': 'L', 'totalShares': 11039152000}] 

請注意,序列化字典中元素的順序在兩個Python版本之間已更改,但字典相同。

要訪問「數據」塊內的各個項目,請使用[0]描述符從塊和各自的描述符中提取字典。例如

print(s["data"][0]["secID"]) 

將打印

000002.XSHE 
+0

所以我試過了,但正如@Thaian所說,只有數據字典,但理想情況下我需要所有後續的東西,即secID,ticker ...等 – Liam

+0

@Liam以獲取'data'元素的單個項目它們是'print(s [「data」] [0] [「secID」])中的一個鍵' –

+0

正是我需要的,非常有幫助! – Liam