bugfix> python > 投稿

私はこのスクリプトを数日間使用しており、返された book_ids を期待していましたおよび type_odds 私の csv では毎日同じ場所にいるでしょうインポート後に E6 を返すことができますで cellexcel book_id のみを取得する21のエントリー..それは明らかにそれを行う正しい方法ではありません。

私がやろうとしていることは、 book_id の場合に以下の文字列変数のみを書き込み/印刷することですis = to 21および type_odds is =ゲームへ。

import csv
import requests
import datetime
from pprint import pprint
outfile = open("BOOKS.csv","w",newline='')
writer = csv.writer(outfile)
writer.writerow(["book_ids","status","away_team_id","away_rot","ml_away","spread_away","spread_away_line","home_team_id",
                 "home_rot","ml_home","spread_home","spread_home_line","over","under","total","type_odds","insert","time",
                 "one_tm_id", "one_team", "one_tm", "two_tm_id", "two_team", "two_tm"])
req2 = requests.get('https://api-prod.sprtactn.co/web/v1/scoreboard/mlb?bookIds=21,1,55&date=' + datetime.datetime.now().strftime('%Y%m%d') + '')
odd = req2.json()['games']
for info in odd:
    time = info['start_time']
    status = info['status']
    away_rot = info['away_rotation_number']
    home_rot = info['home_rotation_number']
    away_team_id = info['away_team_id']
    home_team_id = info['home_team_id']
    teams = info['teams']
    vegas = info['odds']
    one_team = teams[0]['full_name']
    one_tm = teams[0]['abbr']
    one_tm_id = teams[0]['id']
    two_team = teams[1]['full_name']
    two_tm = teams[1]['abbr']
    two_tm_id = teams[1]['id']
    for odds in vegas:
        ml_away = odds['ml_away']
        ml_home = odds['ml_home']
        type_odds = odds['type']
        insert = odds['inserted']
        book_ids = odds['book_id']
        spread_away = odds['spread_away']
        spread_home = odds['spread_home']
        spread_away_line = odds['spread_away_line']
        spread_home_line = odds['spread_home_line']
        over = odds['over']
        under = odds['under']
        total = odds['total']

        print(book_ids, status, away_team_id, away_rot, ml_away, spread_away, spread_away_line, 
              home_team_id, home_rot, ml_home, spread_home, spread_home_line,
              over, under, total, type_odds, insert, time, one_tm_id, one_team, one_tm, two_tm_id, two_team, two_tm)
        writer.writerow([book_ids, status, away_team_id, away_rot, ml_away, spread_away, spread_away_line, 
              home_team_id, home_rot, ml_home, spread_home, spread_home_line,
              over, under, total, type_odds, insert, time, one_tm_id, one_team, one_tm, two_tm_id, two_team, two_tm])

回答 1 件
  • 次の行を追加するだけです。

    if book_ids == 21 and type_odds == 'game':
    
    

    たとえば、コードは次のように機能します。

    import csv
    import requests
    import datetime
    from pprint import pprint
    header = ["book_ids","status","away_team_id","away_rot","ml_away","spread_away","spread_away_line","home_team_id",
              "home_rot","ml_home","spread_home","spread_home_line","over","under","total","type_odds","insert","time",
              "one_tm_id", "one_team", "one_tm", "two_tm_id", "two_team", "two_tm"]
    req2 = requests.get('https://api-prod.sprtactn.co/web/v1/scoreboard/mlb?bookIds=21,1,55&date=' + datetime.datetime.now().strftime('%Y%m%d') + '')
    odd = req2.json()['games']
    with open("BOOKS.csv", "w", newline='') as f_output:
        csv_output = csv.writer(f_output)
        csv_output.writerow(header)
        for info in odd:
            time = info['start_time']
            status = info['status']
            away_rot = info['away_rotation_number']
            home_rot = info['home_rotation_number']
            away_team_id = info['away_team_id']
            home_team_id = info['home_team_id']
            teams = info['teams']
            vegas = info['odds']
            one_team = teams[0]['full_name']
            one_tm = teams[0]['abbr']
            one_tm_id = teams[0]['id']
            two_team = teams[1]['full_name']
            two_tm = teams[1]['abbr']
            two_tm_id = teams[1]['id']
            for odds in vegas:
                ml_away = odds['ml_away']
                ml_home = odds['ml_home']
                type_odds = odds['type']
                insert = odds['inserted']
                book_ids = odds['book_id']
                spread_away = odds['spread_away']
                spread_home = odds['spread_home']
                spread_away_line = odds['spread_away_line']
                spread_home_line = odds['spread_home_line']
                over = odds['over']
                under = odds['under']
                total = odds['total']
                row = [book_ids, status, away_team_id, away_rot, ml_away, spread_away, spread_away_line, 
                      home_team_id, home_rot, ml_home, spread_home, spread_home_line,
                      over, under, total, type_odds, insert, time, one_tm_id, one_team, one_tm, two_tm_id, two_team, two_tm]
                if book_ids == 21 and type_odds == 'game':
                    print(', '.join(map(str, row)))
                    csv_output.writerow(row)
    
    

    それはあなたに BOOKS.csv を与えるでしょう  含む:

    book_ids,status,away_team_id,away_rot,ml_away,spread_away,spread_away_line,home_team_id,home_rot,ml_home,spread_home,spread_home_line,over,under,total,type_odds,insert,time,one_tm_id,one_team,one_tm,two_tm_id,two_team,two_tm
    21,scheduled,194,965,230,,,193,966,-280,,,-105,-115,10,game,2018-05-29T13:34:13.125+00:00,2018-05-29T22:10:00.000Z,193,Cleveland Indians,CLE,194,Chicago White Sox,CWS
    21,scheduled,216,979,-105,,,187,980,-115,,,-120,100,9,game,2018-05-29T14:56:44.026+00:00,2018-05-29T23:05:00.000Z,187,Baltimore Orioles,BAL,216,Washington Nationals,WSH
    21,scheduled,206,951,-123,,,203,952,103,,,-110,-110,8.5,game,2018-05-29T14:21:13.357+00:00,2018-05-29T23:05:00.000Z,203,Pittsburgh Pirates,PIT,206,Chicago Cubs,CHC
    21,scheduled,199,967,-120,,,191,968,100,,,-110,-110,9,game,2018-05-29T14:56:44.026+00:00,2018-05-29T23:05:00.000Z,191,New York Yankees,NYY,199,Houston Astros,HOU
    21,scheduled,198,969,-126,,,195,970,106,,,-105,-115,9,game,2018-05-29T14:32:43.572+00:00,2018-05-29T23:10:00.000Z,195,Detroit Tigers,DET,198,Los Angeles Angels,LAA
    21,scheduled,188,971,168,,,190,972,-196,,,-115,-105,9.5,game,2018-05-29T12:47:28.395+00:00,2018-05-29T23:10:00.000Z,188,Toronto Blue Jays,TOR,190,Boston Red Sox,BOS
    21,scheduled,213,953,110,,,215,954,-130,,,100,-120,9,game,2018-05-29T14:43:43.731+00:00,2018-05-29T23:35:00.000Z,213,New York Mets,NYM,215,Atlanta Braves,ATL
    21,scheduled,207,955,-110,,,208,956,-110,,,105,-125,9,game,2018-05-29T13:18:59.068+00:00,2018-05-29T23:40:00.000Z,207,St. Louis Cardinals,STL,208,Milwaukee Brewers,MIL
    21,scheduled,196,973,-134,,,192,974,114,,,100,-120,9,game,2018-05-29T14:52:43.62+00:00,2018-05-30T00:15:00.000Z,192,Kansas City Royals,KC,196,Minnesota Twins,MIN
    21,scheduled,209,957,125,,,210,958,-145,,,-120,100,11,game,2018-05-29T13:32:13.351+00:00,2018-05-30T00:40:00.000Z,209,San Francisco Giants,SF,210,Colorado Rockies,COL
    21,scheduled,202,959,102,,,205,960,-122,,,-125,105,8,game,2018-05-29T14:46:43.602+00:00,2018-05-30T01:40:00.000Z,202,Cincinnati Reds,CIN,205,Arizona Diamondbacks,ARI
    21,scheduled,189,975,-112,,,201,976,-108,,,-105,-115,8,game,2018-05-29T14:52:43.62+00:00,2018-05-30T02:05:00.000Z,189,Tampa Bay Rays,TB,201,Oakland Athletics,OAK
    21,scheduled,200,977,145,,,197,978,-170,,,-120,100,8.5,game,2018-05-29T14:52:43.62+00:00,2018-05-30T02:10:00.000Z,197,Seattle Mariners,SEA,200,Texas Rangers,TEX
    21,scheduled,212,963,153,,,204,964,-178,,,-115,-105,7,game,2018-05-29T13:32:43.146+00:00,2018-05-30T02:10:00.000Z,204,Los Angeles Dodgers,LAD,212,Philadelphia Phillies,PHI
    21,scheduled,214,961,133,,,211,962,-153,,,-120,100,7,game,2018-05-29T14:56:44.026+00:00,2018-05-30T02:10:00.000Z,211,San Diego Padres,SD,214,Miami Marlins,MIA
    
    

あなたの答え