将JSON解析为Python中的平面文件

时间:2019-04-15 19:22:24

标签: python json

我想读取一个密钥不总是相同的JSON格式文件,并将其转换为平面文件以进行进一步处理。我尝试为此找到解决方案,但没有任何效果。

这是示例JSON数据。

{
    "Meta Data": {
        "1. Information": "Intraday (5min) open, high, low, close prices and volume",
        "2. Symbol": "MSFT",
        "3. Last Refreshed": "2019-04-15 13:05:00",
        "4. Interval": "5min",
        "5. Output Size": "Compact",
        "6. Time Zone": "US/Eastern"
    },
    "Time Series (5min)": {
        "2019-04-15 13:05:00": {
            "1. open": "120.8250",
            "2. high": "120.9400",
            "3. low": "120.7800",
            "4. close": "120.9275",
            "5. volume": "152975"
        },
        "2019-04-15 13:00:00": {
            "1. open": "120.8900",
            "2. high": "120.8900",
            "3. low": "120.8000",
            "4. close": "120.8250",
            "5. volume": "156065"
        },
        "2019-04-15 12:55:00": {
            "1. open": "120.8600",
            "2. high": "120.9000",
            "3. low": "120.8450",
            "4. close": "120.8950",
            "5. volume": "53450"
        },
    }
}       

我正在尝试将其转换为以下格式

2019-04-15 13:05:00,120.8250,120.9400,120.7800,120.9275,152975
2019-04-15 13:00:00,120.8900,120.8900,120.8000,120.8250,156065
2019-04-15 12:55:00,120.8600,120.9000,120.8450,120.8950,53450

我尝试了以下类似方法。

url = URL from where data is coming in JSON format.
response = requests.get(url, headers=header, cookies=cookies)
dic = json.loads(response.content)
len_of_timeseries = len(dic["Time Series (1min)"])
key_len = {}
for k in dic["Time Series (1min)"]:
  print (dic["Time Series (1min)"][k])
f = open(symbolFile,"w") 
f.write(write the file here)
f.close

上面的打印语句是如下的打印详细信息。它根本没有读取日期字段。

{'1. open': '11689.7998', '2. high': '11690.6504', '3. low': '11689.3496', '4. close': '11689.5996', '5. volume': '0'}
{'1. open': '11687.2500', '2. high': '11690.2002', '3. low': '11686.6504', '4. close': '11689.3496', '5. volume': '0'}

任何帮助将不胜感激。

谢谢

2 个答案:

答案 0 :(得分:0)

for循环中的k值包含您要查找的日期:

for k in dic["Time Series (1min)"]:
    print (dic["Time Series (1min)"][k])
    print(k) # this will print the date

答案 1 :(得分:0)

dic = {
    "Meta Data": {
        "1. Information": "Intraday (5min) open, high, low, close prices and volume",
        "2. Symbol": "MSFT",
        "3. Last Refreshed": "2019-04-15 13:05:00",
        "4. Interval": "5min",
        "5. Output Size": "Compact",
        "6. Time Zone": "US/Eastern"
    },
    "Time Series (5min)": {
        "2019-04-15 13:05:00": {
            "1. open": "120.8250",
            "2. high": "120.9400",
            "3. low": "120.7800",
            "4. close": "120.9275",
            "5. volume": "152975"
        },
        "2019-04-15 13:00:00": {
            "1. open": "120.8900",
            "2. high": "120.8900",
            "3. low": "120.8000",
            "4. close": "120.8250",
            "5. volume": "156065"
        },
        "2019-04-15 12:55:00": {
            "1. open": "120.8600",
            "2. high": "120.9000",
            "3. low": "120.8450",
            "4. close": "120.8950",
            "5. volume": "53450"
        },
    }
}

items = dic["Time Series (5min)"]

for dt, v in items.items():
    row = [dt] # date time
    row += v.values()
    print(','.join(row))

结果

2019-04-15 13:05:00,120.8250,120.9400,120.7800,120.9275,152975
2019-04-15 13:00:00,120.8900,120.8900,120.8000,120.8250,156065
2019-04-15 12:55:00,120.8600,120.9000,120.8450,120.8950,53450

但是它可能不遵守行中的顺序。
如果您知道键"1. open""2. high"等,那么最好使用它们来保持顺序。

for dt, v in items.items():
    row = [dt, v["1. open"], v["2. high"], v["3. low"], v["4. close"], v["5. volume"]]
    print(','.join(row))