我从API下载了一个字符串:
{"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
之后的东西,它看起来像一本字典。
如何获取字符串的那部分并将其转换为实际的字典?
答案 0 :(得分:2)
你可以使用python标准库中的json.loads:
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'}
如果您需要从dict中提取endDate值,请使用:
json.loads(s)['data'][0]['endDate']
Convert a String representation of a Dictionary to a dictionary?
答案 1 :(得分: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'}]
在支持Unicode开箱即用的Python 3中,它将打印
[{'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版本之间发生了变化,但字典是相同的。
要访问“data”块中的单个项目,请使用[0]
描述符从块中提取字典和相应的描述符。例如
print(s["data"][0]["secID"])
会打印
000002.XSHE