Python脚本继续通过null

时间:2017-12-15 02:58:01

标签: python null

我遇到了一个问题,找到了适当的文档。我的脚本将一直运行,直到它达到播放器的空值,如下所示,而不是停止。我认为import json, hmac, hashlib, time, requests, base64 from requests.auth import AuthBase # Create custom authentication for Exchange class CoinbaseExchangeAuth(AuthBase): def __init__(self, api_key, secret_key, passphrase): self.api_key = api_key self.secret_key = secret_key self.passphrase = passphrase def __call__(self, request): timestamp = str(time.time()) message = timestamp + request.method + request.path_url + (request.body or b'').decode() hmac_key = base64.b64decode(self.secret_key) signature = hmac.new(hmac_key, message.encode(), hashlib.sha256) signature_b64 = base64.b64encode(signature.digest()).decode() request.headers.update({ 'CB-ACCESS-SIGN': signature_b64, 'CB-ACCESS-TIMESTAMP': timestamp, 'CB-ACCESS-KEY': self.api_key, 'CB-ACCESS-PASSPHRASE': self.passphrase, 'Content-Type': 'application/json' }) return request api_url = 'https://api.gdax.com/' auth = CoinbaseExchangeAuth(APIKEY, API_SECRET, API_PASS) # Get accounts r = requests.get(api_url + 'accounts', auth=auth) print(r.json()) # [{"id": "a1b2c3d4", "balance":... # Place an order order = { 'size': 1.0, 'price': 1.0, 'side': 'buy', 'product_id': 'BTC-USD', } r = requests.post(api_url + 'orders', json=order, auth=auth) print(r.json()) if statement中需要有一个Player我正在努力寻找正确的方法来编写它并找到我正在寻找的内容。

for loop

以下是json的样子:

    import base64
    import requests
    import json
    import csv

    USERNAME, PASSWORD = 'Name', 'Pass'

    header = ["Update", "Home_Abbrev", "Home_Pos", "Home_First", "Home_Last", "Away_Abbrev", "Away_Pos", "Away_First", "Away_Last"]
    headers = {
            "Authorization": "Basic " +
                    base64.b64encode('{}:{}'.format(USERNAME,PASSWORD)\
                                    .encode('utf-8')).decode('ascii')
        }

    with open("GameDayLines.csv", 'w', newline='') as f:
        writer = csv.writer(f)
        writer.writerow(header)
        for gameid in range(41262,41264):  #ENTER GAME ID'S 12/14/2017
            req = requests.get(url="https://api.mysportsfeeds.com/v1.1/pull/nhl/2017-2018-regular/game_startinglineup.json?gameid=" + str(gameid) , headers=headers)
            req.raise_for_status()
            data = req.json()

            starting_lineup = data['gamestartinglineup']
            update = starting_lineup['lastUpdatedOn']

            away_team_abbrev = starting_lineup['game']['awayTeam']
            home_team_abbrev = starting_lineup['game']['homeTeam']
            away_abbrev = away_team_abbrev['Abbreviation']
            home_abbrev = home_team_abbrev['Abbreviation']

            team_away = starting_lineup['teamLineup'][0]['expected']['starter']
            team_home = starting_lineup['teamLineup'][1]['expected']['starter']

            for i in range(20):
                home_player_first = team_home[i]['player']['FirstName']
                home_player_last = team_home[i]['player']['LastName']
                home_player_pos = team_home[i]['position']

                away_player_first = team_away[i]['player']['FirstName']
                away_player_last = team_away[i]['player']['LastName']
                away_player_pos = team_away[i]['position']                

                writer.writerow([update, home_abbrev, home_player_pos, home_player_first, home_player_last, away_abbrev, away_player_pos, away_player_first, away_player_last])
                print(update, home_abbrev, home_player_pos, home_player_first, home_player_last, away_abbrev, away_player_pos, away_player_first, away_player_last)

1 个答案:

答案 0 :(得分:1)

如果home_player和away_player可以为null,则应在使用前检查它们,如

for i in range(20): 
    if team_home[i]['player'] is not None:
        home_home_player_firstplayer_first = team_home[i]['player']['FirstName'] 
        home_player_last = team_home[i]['player']['LastName'] 
        home_player_pos = team_home[i]['position'] 

    if team_away[i]['player'] is not None:
        away_player_first = team_away[i]['player']['FirstName'] 
        away_player_last = team_away[i]['player']['LastName'] 
        away_player_pos = team_away[i]['position']