Python:从JSON

时间:2018-01-11 02:03:09

标签: python json

我从股票市场历史的API中提取了一组数据。然后将历史存储在文件“history.json”中。我需要获得所有重复的关键字“4. close”。有100天之间&通话中的10年数据。数据的格式如下:

{
    "Meta Data": {
        "1. Information": "Daily Prices (open, high, low, close) and Volumes",
        "2. Symbol": "SPY",
        "3. Last Refreshed": "2018-01-10",
        "4. Output Size": "Compact",
        "5. Time Zone": "US/Eastern"
    },
    "Time Series (Daily)": {
        "2017-08-18": {
            "1. open": "242.9000",
            "2. high": "244.1900",
            "3. low": "242.2000",
            "4. close": "242.7100",
            "5. volume": "136748000"
        },
        "2017-08-21": {
            "1. open": "242.6400",
            "2. high": "243.2000",
            "3. low": "241.8300",
            "4. close": "242.9000",
            "5. volume": "65469700"
        }

优选地,我希望输出按顺序作为列表,其中除了键之外的值作为列表。它可以被调用到内存中或转换成另一个要打开的文件 - 它确实需要保留顺序,因为它将在算法中按顺序使用。 (我试着自己解决问题,我是编程的新手,如果你能尽可能地简化事情,我会很感激。)

2 个答案:

答案 0 :(得分:0)

迈克尔

您可以查看此网址https://docs.python.org/2/library/json.html

使用功能"加载"你可以解析你的json,你得到一个列表作为回报

我希望它有所帮助

答案 1 :(得分:0)

可以将此数据视为嵌套字典并使用它:

outlist = [v["4. close"]   for v in mydict["Time Series (Daily)"].values()]
print(outlist)

输出:

['242.7100', '242.9000']

数据:

mydict = {
    "Meta Data": {
        "1. Information": "Daily Prices (open, high, low, close) and Volumes",
        "2. Symbol": "SPY",
        "3. Last Refreshed": "2018-01-10",
        "4. Output Size": "Compact",
        "5. Time Zone": "US/Eastern"
    },
    "Time Series (Daily)": {
        "2017-08-18": {
            "1. open": "242.9000",
            "2. high": "244.1900",
            "3. low": "242.2000",
            "4. close": "242.7100",
            "5. volume": "136748000"
        },
        "2017-08-21": {
            "1. open": "242.6400",
            "2. high": "243.2000",
            "3. low": "241.8300",
            "4. close": "242.9000",
            "5. volume": "65469700"
        }}}