You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python字典动态取值求助:如何获取Time Series中可变日期对应的open、high、low等值

Hey there, let's figure out how to grab those dynamic low/high values without hardcoding dates! Here are a couple of straightforward solutions:

1. Iterate through all timestamped data

Since the dates are dynamic, you can loop through every entry in the Time Series (1min) dictionary to access the high, low, open, close, and volume values for each time point. This works great if you need to process all available data points:

# First, pull the entire time series section
time_series = data['Time Series (1min)']

# Loop through each timestamp and its corresponding values
for timestamp, metrics in time_series.items():
    high = metrics['2. high']
    low = metrics['3. low']
    open_price = metrics['1. open']
    close_price = metrics['4. close']
    volume = metrics['5. volume']
    
    # Do whatever you need with these values—print, store in a list, etc.
    print(f"At {timestamp}: High = {high}, Low = {low}, Volume = {volume}")

2. Grab the most recent data point

If you only care about the latest entry (which is often the case with time series data), you can either grab the first entry directly (assuming the API returns entries in descending order of time) or sort the timestamps to ensure you get the newest one:

Option A: Quick grab (assuming descending order)

time_series = data['Time Series (1min)']
# Get the first value in the dictionary (latest timestamp if sorted descending)
latest_metrics = next(iter(time_series.values()))

latest_high = latest_metrics['2. high']
latest_low = latest_metrics['3. low']
print(f"Latest High: {latest_high}, Latest Low: {latest_low}")

Option B: Sort timestamps to guarantee the latest

If you're not sure about the order of entries, sort the timestamp keys to get the newest one explicitly:

time_series = data['Time Series (1min)']
# Sort timestamps in reverse order (newest first)
sorted_timestamps = sorted(time_series.keys(), reverse=True)
latest_timestamp = sorted_timestamps[0]

# Pull metrics for the latest timestamp
latest_metrics = time_series[latest_timestamp]
latest_high = latest_metrics['2. high']
latest_low = latest_metrics['3. low']

A quick note on KeyError

Make sure you're using the exact key strings returned by the API—things like '2. high' (with the space and number) are case-sensitive and must match perfectly. That's probably why you ran into KeyError earlier if you tried a simplified key like 'high'!

内容的提问来源于stack exchange,提问作者Brian Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 06:49:45