Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python将多个请求写入一个Csv_Python_Export To Csv - Fatal编程技术网

使用Python将多个请求写入一个Csv

使用Python将多个请求写入一个Csv,python,export-to-csv,Python,Export To Csv,我在组合两个脚本时遇到了一个问题。我试着把writer.writerow放在几个不同的地方,但我已经没有主意了。从输出中可以看到,它循环最后一个循环,并重复前两个循环,直到第三个循环完成,然后移动到第二个循环。谢谢你看 import csv import requests import os outfile = open("NHL_Home_Skater.csv","a",newline='') writer = csv.writer(outfile) writer.writerow(["Da

我在组合两个脚本时遇到了一个问题。我试着把
writer.writerow
放在几个不同的地方,但我已经没有主意了。从输出中可以看到,它循环最后一个循环,并重复前两个循环,直到第三个循环完成,然后移动到第二个循环。谢谢你看

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()
输出:

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

这个问题并不难,因为它很复杂,但因为数据量很大(将近10000行数据),加上每行大约有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列的行作为输出,并且每个循环只写一次
  • 只有1个循环,而不是3个嵌套的循环

感谢您抽出时间查看此内容。除了每个数据行之间的空白行外,一切看起来都很好。我在上面编辑了我的新输出,我想知道是否有办法删除这些空行?现在我明白了为什么在
open()
语句中有
newline=''
。把它放回去就可以了。对于zip()中的项目,
中的
zip
是什么意思?
很抱歉回复太晚了,但这里是zip函数的文档,它也有示例。