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

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

Find latest date value in a dictionary

问题

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

{u'2019-10-08':

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

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

  1. import requests,json
  2. func = 'function=TIME_SERIES_DAILY'
  3. sym = 'symbol=MSFT'
  4. inter = 'interval=10min'
  5. apikey = 'apikey=**********'
  6. url = 'https://www.alphavantage.co/query?'+func+'&'+sym+'&'+inter+'&'+apikey
  7. resp = requests.get(url)
  8. data = json.loads(resp.content)
  9. d = data['Time Series (Daily)']
  10. # print (d['2020-01-03']) #<- date
  11. # print (max(d.values())) # does not give the expected data (i think because the date is in a string)
  12. print (d)

缩短的结果:

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

  1. import requests,json
  2. func = 'function=TIME_SERIES_DAILY'
  3. sym = 'symbol=MSFT'
  4. inter = 'interval=10min'
  5. apikey = 'apikey=**********'
  6. url = 'https://www.alphavantage.co/query?'+func+'&'+sym+'&'+inter+'&'+apikey
  7. resp = requests.get(url)
  8. data = json.loads(resp.content)
  9. d = data['Time Series (Daily)']
  10. # print (d['2020-01-03']) #<- date
  11. # print (max(d.values())) # does not give the expected data (i think because the date is in a string)
  12. print (d)

Shortened Result:

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

  1. from datetime import datetime
  2. maxdate = max((x for x in d.keys()), key=lambda x: datetime.strptime(x, "%Y-%m-%d"))
  3. print(maxdate)
  4. print(d[maxdate])

这将给我以下输出:

  1. 2020-01-03
  2. {'1. open': '158.3200',
  3. '2. high': '159.9450',
  4. '3. low': '158.0600',
  5. '4. close': '158.6200',
  6. '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).

  1. from datetime import datetime
  2. maxdate = max((x for x in d.keys()), key=lambda x: datetime.strptime(x, "%Y-%m-%d"))
  3. print(maxdate)
  4. print(d[maxdate])

Which gives me output:

  1. 2020-01-03
  2. {'1. open': '158.3200',
  3. '2. high': '159.9450',
  4. '3. low': '158.0600',
  5. '4. close': '158.6200',
  6. '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:

确定