简体   繁体   English

如何检索字符串中的最后两条数据?

[英]How to retrieve last two pieces of data in a string?

I want to retrieve the last two pieces of data from 'flucs' for each 'selection'.我想为每个“选择”从“flucs”中检索最后两条数据。 I have tried using an index, but I don't think indexes can be used on strings.我尝试过使用索引,但我认为索引不能用于字符串。

My code:我的代码:

request2 = requests.get('https://www.punters.com.au/api/web/public/Odds/getOddsComparisonCacheable/?allowGet=true&APIKey=65d5a3e79fcd603b3845f0dc7c2437f0&eventId=1045618&betType=FixedWin', headers={'User-Agent': 'Mozilla/5.0'})
json2 = request2.json()
for selection in json2['selections']:
    for price in selection['prices']:

JSON: JSON:

{
   "eventId":1045618,
   "eventName":"Doomben R1",
   "eventNameFull":"Doomben Race 1 - 1600m",
   "eventDesc":"TAB LONG MAY WE PLAY QTIS Three-Year-Old Handicap",
   "eventDistance":"1600",
   "startTime":1598059980,
   "sportId":1,
   "nativeBetslip":false,
   "showHeader":false,
   "isForm":true,
   "isRacing":true,
   "isGreyhounds":false,
   "isHarness":false,
   "isHorseRacing":true,
   "isHorseRacingFutures":false,
   "resultState":"unresulted",
   "status":"open",
   "excludeQuickBet":false,
   "showTote":true,
   "selections":[
      {
         "name":"Bangers And Mayo",
         "selectionId":"10251639",
         "competitorId":"739220",
         "result":null,
         "resultOrdinal":"th",
         "flucsKey":"10251639-Average-FixedWin",
         "bestOdds":8.6,
         "bestOddsBookie":"SportsBetting",
         "flucs":"[[1597813279,8],[1597957152,8.01],[1597990502,8.05],[1597995079,8.01],[1597995740,8.05],[1597996478,8.01],[1598008530,8.05]]"

You need to convert it to a real list:您需要将其转换为真实列表:

from ast import literal_eval

flucs = "[[1597813279,8],[1597957152,8.01],[1597990502,8.05],[1597995079,8.01],[1597995740,8.05],[1597996478,8.01],[1598008530,8.05]]"

lst = literal_eval(flucs)
print(lst[-2:])

This yields这产生

[[1597996478, 8.01], [1598008530, 8.05]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM