如何实现仅当book_id=21且type_odds=game时写入/打印CSV?
Solution: Filter Rows by
book_id=21 and type_odds=game Got it, let's sort this out for you! The core problem here is that your current script writes all retrieved odds data to the CSV, forcing you to rely on fixed column positions for later filtering—not a reliable approach. Instead, we can add an explicit condition to only print/write the rows that match your exact criteria.
Here's the modified script with the filtering logic added:
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'] # Add this condition to filter only the rows you want if book_ids == 21 and type_odds == 'game': 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]) # Don't forget to close the file to prevent data loss outfile.close()
Key Changes Explained:
- Added an
ifstatement right before the print/write operations:if book_ids == 21 and type_odds == 'game': - This ensures only rows that meet both criteria are printed and saved to your CSV
- Added
outfile.close()at the end to properly close the file handle (a good practice to avoid potential data corruption)
Now your CSV will only contain the exact entries you need, so you won't have to rely on fragile column position filtering in Excel anymore.
内容的提问来源于stack exchange,提问作者Michael T Johnson
相关产品推荐
相关产品推荐

