在字典中找到最新的日期值。

huangapple go评论93阅读模式
英文:

Find latest date value in a dictionary

问题

我有这段代码,用于请求股票MSFT(Microsoft)的信息,请求成功并获取到所需的所有数据,但是我想要获取最新的股票价格(在Python中,数据以最新到最旧的方式排序,但我不想从顶部获取数据,因为某些语言没有为我排序)。在下面给出的结果中,有多个日期,例如:

{u'2019-10-08':

现在我想要在JSON数据中找到最新的日期,并打印出来。

我正在使用Alpha Advantage获取这些数据,给定的数据没有问题。

import requests,json

func = 'function=TIME_SERIES_DAILY'
sym = 'symbol=MSFT'
inter = 'interval=10min'
apikey = 'apikey=**********'
url = 'https://www.alphavantage.co/query?'+func+'&'+sym+'&'+inter+'&'+apikey

resp = requests.get(url)
data = json.loads(resp.content)
d = data['Time Series (Daily)']

# print (d['2020-01-03']) #<- date
# print (max(d.values()))  # does not give the expected data (i think because the date is in a string)
    
print (d)

缩短的结果:

{u'Meta Data': {u'1. Information': u'Daily Prices (open, high, low, close) and Volumes', u'4. 
Output Size': u'Compact', u'5. Time Zone': u'US/Eastern', u'2. Symbol': u'MSFT', u'3. Last 
Refreshed': u'2020-01-03'}, u'Time Series (Daily)': {u'2019-10-08': {u'5. volume': u'26783336',
 u'4. close': u'135.6700', u'2. high': u'137.7600', u'1. open': u'137.0800', u'3. low': u'135.6200'}}

我已经查看了许多答案,其中一些没有答案,另一些答案使用不同的语言,我已经花了一些时间尝试找到解决方案。

英文:

I have this code that requests the information of the stock MSFT (Microsoft), the request goes right and i get all the data i need on it, however it i want to get the latest stock price (in python it sorts it out latest to oldest however i do not want to take the data off the top because some languages do not sort it out for me) in the result given below, there are multiple dates eg:

{u'2019-10-08':

Now i want to find the latest date in the json data and print THAT out.

I am using Alpha advantage to get this data, there is nothing wrong with the given data.

import requests,json

func = 'function=TIME_SERIES_DAILY'
sym = 'symbol=MSFT'
inter = 'interval=10min'
apikey = 'apikey=**********'
url = 'https://www.alphavantage.co/query?'+func+'&'+sym+'&'+inter+'&'+apikey

resp = requests.get(url)
data = json.loads(resp.content)
d = data['Time Series (Daily)']

# print (d['2020-01-03']) #<- date
# print (max(d.values()))  # does not give the expected data (i think because the date is in a string)
    
print (d)

Shortened Result:

{u'Meta Data': {u'1. Information': u'Daily Prices (open, high, low, close) and Volumes', u'4. 
Output Size': u'Compact', u'5. Time Zone': u'US/Eastern', u'2. Symbol': u'MSFT', u'3. Last 
Refreshed': u'2020-01-03'}, u'Time Series (Daily)': {u'2019-10-08': {u'5. volume': u'26783336',
 u'4. close': u'135.6700', u'2. high': u'137.7600', u'1. open': u'137.0800', u'3. low': u'135.6200'}}

I have looked through many answers and some of them unanswered or few in different languages, I have spend some time trying to get the solution to this.

答案1

得分: 3

如果我正确理解你的问题,你可以使用datetime.strptime()来执行以下操作。请注意,在你的情况下,正如 @Iguananaut 指出的那样,日期格式是按词典顺序排列的,所以你可以直接使用 maxdate = max(d)

from datetime import datetime

maxdate = max((x for x in d.keys()), key=lambda x: datetime.strptime(x, "%Y-%m-%d"))

print(maxdate)
print(d[maxdate])

这将给我以下输出:

2020-01-03
{'1. open': '158.3200',
 '2. high': '159.9450',
 '3. low': '158.0600',
 '4. close': '158.6200',
 '5. volume': '21121681'}
英文:

If I understand the question correctly, you could do the following using datetime.strptime(). Note that in your case, as @Iguananaut pointed out, the date format is in lexicographical order, so you could just use maxdate = max(d).

from datetime import datetime

maxdate = max((x for x in d.keys()), key=lambda x: datetime.strptime(x, "%Y-%m-%d"))

print(maxdate)
print(d[maxdate])

Which gives me output:

2020-01-03
{'1. open': '158.3200',
 '2. high': '159.9450',
 '3. low': '158.0600',
 '4. close': '158.6200',
 '5. volume': '21121681'}

huangapple
  • 本文由 发表于 2020年1月6日 20:33:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612212.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定