具有多个词典的Python拆分字符串

时间:2015-01-14 08:49:51

标签: python json dictionary

所以我正在尝试解析Google Financial API中的数据。我有它为单股票报价工作,但对于多股票报价,它将无法正常工作。 json loads不适用于多个词典。

import urllib.request
import json
import time

def get_stock(url, y):
    data = urllib.request.urlopen(url)
    read = data.read().decode('UTF-8')
    json_data = json.loads(read[5:-2])
    for n in range(int(len(y.replace(",", ""))/4)):
        print(y[4*n:4*n+4].upper(), json_data['l'])

x = ''
while x != 'exit':
    x = input("Enter how often, in minutes (minimum one minute), you want the stock price to be updated; or type 'exit': ")
    z = input("Enter the market that your stock(s) are in: ")
    y = input("Enter the stock(s) that you want to retrieve information for (use commas if neccesary); or type 'exit': ")
    y = y.replace(" ", "")
    url = 'http://finance.google.com/finance/info?client=ig&q=%s:%s' % (z, y)
    while True:
        get_stock(url, y)
        leave = input("Type 'exit' to leave, or wait until the next quote: ")
        if leave == 'exit':
            break
        time.sleep(int(x))

以下是Google API的JSON输出示例:

{
"id": "304466804484872"
,"t" : "GOOG"
,"e" : "NASDAQ"
,"l" : "496.18"
,"l_fix" : "496.18"
,"l_cur" : "496.18"
,"s": "2"
,"ltt":"4:14PM EST"
,"lt" : "Jan 13, 4:14PM EST"
,"lt_dts" : "2015-01-13T16:14:24Z"
,"c" : "+3.63"
,"c_fix" : "3.63"
,"cp" : "0.74"
,"cp_fix" : "0.74"
,"ccol" : "chg"
,"pcls_fix" : "492.55"
,"el": "497.00"
,"el_fix": "497.00"
,"el_cur": "497.00"
,"elt" : "Jan 13, 7:59PM EST"
,"ec" : "+0.82"
,"ec_fix" : "0.82"
,"ecp" : "0.17"
,"ecp_fix" : "0.17"
,"eccol" : "chg"
,"div" : ""
,"yld" : ""
}
,{
"id": "22144"
,"t" : "AAPL"
,"e" : "NASDAQ"
,"l" : "110.22"
,"l_fix" : "110.22"
,"l_cur" : "110.22"
,"s": "2"
,"ltt":"4:14PM EST"
,"lt" : "Jan 13, 4:14PM EST"
,"lt_dts" : "2015-01-13T16:14:23Z"
,"c" : "+0.97"
,"c_fix" : "0.97"
,"cp" : "0.89"
,"cp_fix" : "0.89"
,"ccol" : "chg"
,"pcls_fix" : "109.25"
,"el": "110.30"
,"el_fix": "110.30"
,"el_cur": "110.30"
,"elt" : "Jan 13, 7:59PM EST"
,"ec" : "+0.08"
,"ec_fix" : "0.08"
,"ecp" : "0.07"
,"ecp_fix" : "0.07"
,"eccol" : "chg"
,"div" : "0.47"
,"yld" : "1.71"
}

我似乎无法找到有效的解决方案。 split方法不起作用,因为字符串中的逗号无处不在。由于linesplit多次换行,\n无效。

我只需要将字符串拆分,以便我可以使用json dumps,然后使用json loads对每个字符串进行迭代,以解析数据以进行访问。

1 个答案:

答案 0 :(得分:3)

只需添加几个括号就可以了:

>>> import json
>>> src = """{
"id": "304466804484872"
,"t" : "GOOG"
,"e" : "NASDAQ"
,"l" : "496.18"
,"l_fix" : "496.18"
,"l_cur" : "496.18"
,"s": "2"
,"ltt":"4:14PM EST"
,"lt" : "Jan 13, 4:14PM EST"
,"lt_dts" : "2015-01-13T16:14:24Z"
,"c" : "+3.63"
,"c_fix" : "3.63"
,"cp" : "0.74"
,"cp_fix" : "0.74"
,"ccol" : "chg"
,"pcls_fix" : "492.55"
,"el": "497.00"
,"el_fix": "497.00"
,"el_cur": "497.00"
,"elt" : "Jan 13, 7:59PM EST"
,"ec" : "+0.82"
,"ec_fix" : "0.82"
,"ecp" : "0.17"
,"ecp_fix" : "0.17"
,"eccol" : "chg"
,"div" : ""
,"yld" : ""
}
,{
"id": "22144"
,"t" : "AAPL"
,"e" : "NASDAQ"
,"l" : "110.22"
,"l_fix" : "110.22"
,"l_cur" : "110.22"
,"s": "2"
,"ltt":"4:14PM EST"
,"lt" : "Jan 13, 4:14PM EST"
,"lt_dts" : "2015-01-13T16:14:23Z"
,"c" : "+0.97"
,"c_fix" : "0.97"
,"cp" : "0.89"
,"cp_fix" : "0.89"
,"ccol" : "chg"
,"pcls_fix" : "109.25"
,"el": "110.30"
,"el_fix": "110.30"
,"el_cur": "110.30"
,"elt" : "Jan 13, 7:59PM EST"
,"ec" : "+0.08"
,"ec_fix" : "0.08"
,"ecp" : "0.07"
,"ecp_fix" : "0.07"
,"eccol" : "chg"
,"div" : "0.47"
,"yld" : "1.71"
}"""

>>> json.loads(src)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/json/decoder.py", line 369, in decode
    raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 30 column 1 - line 58 column 2 (char 512 - 1022)
>>> src = u"[%s]" % src
>>> json.loads(src)
[{u'el': u'497.00', u'eccol': u'chg', u'ec': u'+0.82', u'l_fix': u'496.18', u'cp': u'0.74', u'id': u'304466804484872', u'yld': u'', u'el_fix': u'497.00', u'lt': u'Jan 13, 4:14PM EST', u'ecp_fix': u'0.17', u'elt': u'Jan 13, 7:59PM EST', u'cp_fix': u'0.74', u'c_fix': u'3.63', u'pcls_fix': u'492.55', u'ecp': u'0.17', u'div': u'', u'l_cur': u'496.18', u'c': u'+3.63', u'e': u'NASDAQ', u'ltt': u'4:14PM EST', u'l': u'496.18', u's': u'2', u't': u'GOOG', u'el_cur': u'497.00', u'lt_dts': u'2015-01-13T16:14:24Z', u'ec_fix': u'0.82', u'ccol': u'chg'}, {u'el': u'110.30', u'eccol': u'chg', u'ec': u'+0.08', u'l_fix': u'110.22', u'cp': u'0.89', u'id': u'22144', u'yld': u'1.71', u'el_fix': u'110.30', u'lt': u'Jan 13, 4:14PM EST', u'ecp_fix': u'0.07', u'elt': u'Jan 13, 7:59PM EST', u'cp_fix': u'0.89', u'c_fix': u'0.97', u'pcls_fix': u'109.25', u'ecp': u'0.07', u'div': u'0.47', u'l_cur': u'110.22', u'c': u'+0.97', u'e': u'NASDAQ', u'ltt': u'4:14PM EST', u'l': u'110.22', u's': u'2', u't': u'AAPL', u'el_cur': u'110.30', u'lt_dts': u'2015-01-13T16:14:23Z', u'ec_fix': u'0.08', u'ccol': u'chg'}]
>>> 
相关问题