2017-12-20 3 views
1

몇 개의 스크립트를 결합하는 데 문제가 있습니다. 나는 두 개의 다른 장소에 writer.writerow을 배치하려했지만 아이디어가 부족했습니다. 출력에서 볼 수 있듯이 마지막 루프를 반복하고 3 번째가 완료 될 때까지 처음 두 개의 루프를 반복하여 두 번째 루프로 이동합니다. 좀 봐 주셔서 감사합니다!Python을 사용하여 하나의 Csv에 여러 요청 작성

import csv 
import requests 
import os 

outfile = open("NHL_Home_Skater.csv","a",newline='') 
writer = csv.writer(outfile) 
writer.writerow(["Date","Player","PlayerId","Pos","TM","Opp","GameId","GP","G","A","Points","Shots","Spctg","PPG","PPP","SHG","SHP","TOI","Shifts","PIM","Player","GameId","BS","Give Aways","Take Aways","Missed Shots","Player","GameId","EvTOI","PpTOI","ShTOI"]) 

req = requests.get('http://www.nhl.com/stats/rest/skaters?isAggregate=false&reportType=basic&isGame=true&reportName=skatersummary&sort=[{%22property%22:%22playerName%22,%22direction%22:%22ASC%22}]&factCayenneExp=gamesPlayed%3E=1&cayenneExp=gameDate%3E=%222017-10-04%22%20and%20gameDate%3C=%222017-12-31%22%20and%20gameTypeId=2%20and%20gameLocationCode=%22H%22') 
data = req.json()['data'] 
for item in data: 
    Player = item['playerName'] 
    date = item['gameDate'] 
    Pos = item['playerPositionCode'] 
    TM = item['teamAbbrev'] 
    Opp = item['opponentTeamAbbrev'] 
    GameId = item['gameId'] 
    GP = item['gamesPlayed'] 
    G = item['goals'] 
    A = item['assists'] 
    Shots = item['shots'] 
    PIM = item['penaltyMinutes'] 
    PlayerId = item['playerId'] 
    Points = item['points'] 
    PPG = item['ppGoals'] 
    PPP = item['ppPoints'] 
    SHG = item['shGoals'] 
    SHP = item['shPoints'] 
    Shifts = item['shiftsPerGame'] 
    Spctg = item['shootingPctg'] 
    TOI = item['timeOnIcePerGame'] 

    writer.writerow([date,Player,PlayerId,Pos,TM,Opp,GameId,GP,G,A,Points,Shots,Spctg,PPG,PPP,SHG,SHP,TOI,Shifts,PIM]) 

    req2 = requests.get('http://www.nhl.com/stats/rest/skaters?isAggregate=false&reportType=basic&isGame=true&reportName=realtime&sort=[{%22property%22:%22playerName%22,%22direction%22:%22ASC%22}]&factCayenneExp=gamesPlayed%3E=1&cayenneExp=gameDate%3E=%222017-10-04%22%20and%20gameDate%3C=%222017-12-31%22%20and%20gameTypeId=2%20and%20gameLocationCode=%22H%22') 
    data2 = req2.json()['data'] 
    for item in data2: 
     Player2 = item['playerName'] 
     GameId2 = item['gameId'] 
     BS = item['blockedShots'] 
     GiveAways = item['giveaways'] 
     TakeAways = item['takeaways'] 
     MissedShots = item['missedShots'] 

     writer.writerow([Player,GameId,BS,GiveAways,TakeAways,MissedShots]) 

     req3 = requests.get('http://www.nhl.com/stats/rest/skaters?isAggregate=false&reportType=basic&isGame=true&reportName=timeonice&sort=[{%22property%22:%22playerName%22,%22direction%22:%22ASC%22}]&factCayenneExp=gamesPlayed%3E=1&cayenneExp=gameDate%3E=%222017-10-04%22%20and%20gameDate%3C=%222017-12-31%22%20and%20gameTypeId=2%20and%20gameLocationCode=%22H%22') 
     data3 = req3.json()['data'] 
     for item in data3: 
      Player3 = item['playerName'] 
      GameId3 = item['gameId'] 
      EvTOI = item['evTimeOnIce'] 
      PpTOI = item['ppTimeOnIce'] 
      ShTOI = item['shTimeOnIce'] 

      writer.writerow([Player, GameId, EvTOI, PpTOI, ShTOI]) 
outfile.close() 

OUPUT :

2017-10-28T23:00:00Z A.J. Greer 8478421 L COL CHI 2017020165 1 0 0 0 0 0.0 0 0 0 0 373.0 10.0 2 A.J. Greer 2017020165 0 0 1 0 A.J. Greer 2017020165 373 0 0 
2017-10-28T23:00:00Z A.J. Greer 8478421 L COL CHI 2017020165 1 0 0 0 0 0.0 0 0 0 0 373.0 10.0 2 A.J. Greer 2017020165 0 0 1 0 A.J. Greer 2017020194 486 0 0 
2017-10-28T23:00:00Z A.J. Greer 8478421 L COL CHI 2017020165 1 0 0 0 0 0.0 0 0 0 0 373.0 10.0 2 A.J. Greer 2017020165 0 0 1 0 A.J. Greer 2017020134 498 4 0 
2017-10-28T23:00:00Z A.J. Greer 8478421 L COL CHI 2017020165 1 0 0 0 0 0.0 0 0 0 0 373.0 10.0 2 A.J. Greer 2017020165 0 0 1 0 Aaron Ekblad 2017020190 1035 88 126 

새로운 출력 :이 문제는 복잡하기 어려운 것이 아니라

Date Player PlayerId Pos TM Opp GameId GP G A Points Shots Spctg PPG PPP SHG SHP TOI Shifts PIM 

2017-11-02T23:00:00Z A.J. Greer 8478421 L COL CAR 2017020194 1 0 1 1 0 0 0 0 0 0 486 12 0 

2017-10-28T23:00:00Z A.J. Greer 8478421 L COL CHI 2017020165 1 0 0 0 0 0 0 0 0 0 373 10 2 

2017-10-24T23:00:00Z A.J. Greer 8478421 L COL DAL 2017020134 1 0 0 0 2 0 0 0 0 0 502 13 0 

답변

1

데이터의 양이 많은 (거의 때문에 10,000 행의 데이터), 각 행에는 약 30 개 이상의 열이 있습니다. 귀하의 코드를 읽은 후에, 나는 당신이 당신의 결과물을 위해 무엇을 원하는지 이해합니다 : 3 가지 데이터의 조합. 여기 내 시도 :

import csv 
import os 
import requests 

outfile = open("NHL_Home_Skater.csv", 'a', newline='') 
writer = csv.writer(outfile) 
header=[ 
    "Date","Player","PlayerId","Pos","TM", 
    "Opp","GameId","GP","G","A", 
    "Points","Shots","Spctg","PPG","PPP", 
    "SHG","SHP","TOI", "Shifts", "PIM", 

    "Player2","GameId","BS","Give Aways","Take Aways", "Missed Shots", 
    "Player3","GameId","EvTOI","PpTOI","ShTOI"] 
writer.writerow(header) 

req = requests.get('http://www.nhl.com/stats/rest/skaters?isAggregate=false&reportType=basic&isGame=true&reportName=skatersummary&sort=[{%22property%22:%22playerName%22,%22direction%22:%22ASC%22}]&factCayenneExp=gamesPlayed%3E=1&cayenneExp=gameDate%3E=%222017-10-04%22%20and%20gameDate%3C=%222017-12-31%22%20and%20gameTypeId=2%20and%20gameLocationCode=%22H%22') 
data = req.json()['data'] 

req2 = requests.get('http://www.nhl.com/stats/rest/skaters?isAggregate=false&reportType=basic&isGame=true&reportName=realtime&sort=[{%22property%22:%22playerName%22,%22direction%22:%22ASC%22}]&factCayenneExp=gamesPlayed%3E=1&cayenneExp=gameDate%3E=%222017-10-04%22%20and%20gameDate%3C=%222017-12-31%22%20and%20gameTypeId=2%20and%20gameLocationCode=%22H%22') 
data2 = req2.json()['data'] 

req3 = requests.get('http://www.nhl.com/stats/rest/skaters?isAggregate=false&reportType=basic&isGame=true&reportName=timeonice&sort=[{%22property%22:%22playerName%22,%22direction%22:%22ASC%22}]&factCayenneExp=gamesPlayed%3E=1&cayenneExp=gameDate%3E=%222017-10-04%22%20and%20gameDate%3C=%222017-12-31%22%20and%20gameTypeId=2%20and%20gameLocationCode=%22H%22') 
data3 = req3.json()['data'] 

for item, item2, item3 in zip(data, data2, data3): 
    row = (
     item['gameDate'], 
     item['playerName'], 
     item['playerId'], 
     item['playerPositionCode'], 
     item['teamAbbrev'], 
     item['opponentTeamAbbrev'], 
     item['gameId'], 
     item['gamesPlayed'], 
     item['goals'], 
     item['assists'], 
     item['points'], 
     item['shots'], 
     item['shootingPctg'], 
     item['ppGoals'], 
     item['ppPoints'], 
     item['shGoals'], 
     item['shPoints'], 
     item['timeOnIcePerGame'], 
     item['shiftsPerGame'], 
     item['penaltyMinutes'], 

     item2['playerName'], 
     item2['gameId'], 
     item2['blockedShots'], 
     item2['giveaways'], 
     item2['takeaways'], 
     item2['missedShots'], 

     item3['playerName'], 
     item3['gameId'], 
     item3['evTimeOnIce'], 
     item3['ppTimeOnIce'], 
     item3['shTimeOnIce'], 
     ) 
    writer.writerow(row) 

노트

  • 이 솔루션은 코드에
  • 을 기반으로 요청 수천만에 반대 단지 3 요청하게, 나는 가정 그 3 개 데이터 세트의 행 동일한 순서로되어있어 동일한 플레이어 ID 및 게임 ID를 나타냅니다.
  • 3 개 데이터 세트의 각 행에 대해 31 개의 출력 행을 생성하고 루프 당 한 번만 출력합니다.
  • 중첩 된 루프가 아닌 루프가 하나뿐입니다.
+0

이 문제를 조사해 주셔서 감사합니다. 각 데이터 행 사이의 빈 행을 제외한 모든 것이 훌륭하게 보입니다. 위의 새 Ouput을 편집하고 빈 행을 제거 할 수있는 방법이 있는지 궁금합니다. –

+0

이제'open()'문에 왜'newline = '''이 있는지 이해합니다. 그것을 다시 넣어 당신은 설정됩니다. –

+0

나도 그래, 하이 감사합니다! –

관련 문제