【问题标题】:How can I print the dates of this nested dictionary in Python?如何在 Python 中打印这个嵌套字典的日期?
【发布时间】:2018-09-24 10:51:04
【问题描述】:

如何仅打印/访问“时间序列(每日)”的日期?

我尝试了以下方法,但出现关键错误:

for date in range(0,2):
    print(date)
    print(tickerData['Time Series (Daily)'][date]) 

这是嵌套字典:

   {
        "Meta Data": {
            "1. Information": "Daily Prices (open, high, low, close) and Volumes",
            "2. Symbol": "NASDAQ Index",
            "3. Last Refreshed": "2018-04-13",
            "4. Output Size": "Compact",
            "5. Time Zone": "US/Eastern"
        },
        "Time Series (Daily)": {
            "2018-04-13": {
                "1. open": "7179.6201",
                "2. high": "7183.6201",
                "3. low": "7078.1401",
                "4. close": "7106.6499",
                "5. volume": "1743640000"
            },
            "2018-04-12": {
                "1. open": "7112.0200",
                "2. high": "7166.0000",
                "3. low": "7105.0898",
                "4. close": "7140.2500",
                "5. volume": "2021110000"
            }}}

【问题讨论】:

    标签: python-3.x dictionary nested


    【解决方案1】:

    您可以使用以下命令获取字典中的所有日期:

        dates = tickerdata["Time Series (Daily)"]
        for item in dates:
            print (item)
    

    输出:

    2018-04-13
    2018-04-12
    

    以下评论:

    不确定这是否是您想要的,但您可以执行以下操作:

    class AttributeDict(dict):
        __getattr__ = dict.__getitem__
        __setattr__ = dict.__setitem__
    tickerdata = {
            "Meta Data": {
                "1. Information": "Daily Prices (open, high, low, close) and Volumes",
                "2. Symbol": "NASDAQ Index",
                "3. Last Refreshed": "2018-04-13",
                "4. Output Size": "Compact",
                "5. Time Zone": "US/Eastern"
            },
            "TimeSeriesDaily": {
                "2018-04-13": {
                    "1. open": "7179.6201",
                    "2. high": "7183.6201",
                    "3. low": "7078.1401",
                    "4. close": "7106.6499",
                    "5. volume": "1743640000"
                },
                "2018-04-12": {
                    "1. open": "7112.0200",
                    "2. high": "7166.0000",
                    "3. low": "7105.0898",
                    "4. close": "7140.2500",
                    "5. volume": "2021110000"
                }}}
    
    
    tmp = AttributeDict(tickerdata)
    for date in tmp.'TimeSeriesDaily':
        print(date)
    

    但如果您注意到您的“时间序列(每日)”标签需要简化,我认为您可能无法控制或不想这样做。

    【讨论】:

    • 啊,这很漂亮!碰巧,您知道是否可以直接访问日期而无需拉出“时间序列(每日)”对象?
    • 我不认为这是可能的,但可以做的事情仍然不是很确定这就是你想要的。
    【解决方案2】:
    tickerData ={
        "Meta Data": {
            "1. Information": "Daily Prices (open, high, low, close) and Volumes",
            "2. Symbol": "NASDAQ Index",
            "3. Last Refreshed": "2018-04-13",
            "4. Output Size": "Compact",
            "5. Time Zone": "US/Eastern"
        },
        "Time Series (Daily)": {
            "2018-04-13": {
                "1. open": "7179.6201",
                "2. high": "7183.6201",
                "3. low": "7078.1401",
                "4. close": "7106.6499",
                "5. volume": "1743640000"
            },
            "2018-04-12": {
                "1. open": "7112.0200",
                "2. high": "7166.0000",
                "3. low": "7105.0898",
                "4. close": "7140.2500",
                "5. volume": "2021110000"
            }
        }
    }
    
    for date in tickerData['Time Series (Daily)'].keys():
        print(date)
    

    【讨论】:

      猜你喜欢
      • 2017-03-09
      • 2022-12-03
      • 2019-08-19
      • 2018-09-12
      • 2016-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多