将CSV文件1列操作为多个 - NFL分数

时间:2013-09-03 19:55:48

标签: python csv

使用可以帮助我自动评分游戏的NFL CSV文件。现在,我可以将团队和分数上传到csv文件的唯一一列。

这些都是一列

实施例:         甲

1   NYJ
2   27
3   PHI
4   20
5   BUF
6   13
7   DET
8   35
9   CIN
10  27
11  IND
12  10
13  MIA
14  24
15  NO
16  21

OR

[['NYJ`'], ['27'], ['PHI'], ['20'], ['BUF'], ['13'], ['DET'], ['35'], ['CIN'], ['27'], ['IND'], ['10'], ['MIA'], ['24'], ['NO'], ['21'], ['TB'], ['12'], ['WAS'], ['30'], ['CAR'], ['25'], ['PIT'], ['10'], ['ATL'], ['16'], ['JAC'], ['20'], ['NE'], ['28'], ['NYG'], ['20'], ['MIN'], ['24'], ['TEN'], ['23'], ['STL'], ['24'], ['BAL'], ['21'], ['CHI'], ['16'], ['CLE'], ['18'], ['KC'], ['30'], ['GB'], ['8'], ['DAL'], ['6'], ['HOU'], ['24'], ['DEN'], ['24'], ['ARI'], ['32'], ['SD'], ['6'`], ['SF'], ['41'], ['SEA'], ['22'], ['OAK'], ['6']]

我想要的是:

   A  B   C  D
1 NYJ 27 PHI 20
2 BUF 13 DET 35
3 CIN 27 IND 10
4 MIA 24 NO  21

我已经阅读了之前的文章并且还没有让它工作。关于这个的任何想法?

感谢任何帮助,谢谢!

当前脚本:

import nflgame
import csv
print "Purpose of this script is to get NFL Scores to help out with GUT"

pregames = nflgame.games(2013, week=[4], kind='PRE')

out = open("scores.csv", "wb")
output = csv.writer(out)

for score in pregames:
    output.writerows([[score.home],[score.score_home],[score.away],[score.score_away]])

2 个答案:

答案 0 :(得分:1)

您目前正在使用.writerows()写入4行,每行包含一列。

相反,你想要:

output.writerow([score.home, score.score_home, score.away, score.score_away])

用4列写一行。

答案 1 :(得分:0)

在不知道分数数据的情况下,尝试将writerow更改为writerow:

import nflgame
import csv
print "Purpose of this script is to get NFL Scores to help out with GUT"

pregames = nflgame.games(2013, week=[4], kind='PRE')

out = open("scores.csv", "wb")
output = csv.writer(out)

for score in pregames:
    output.writerow([[score.home],[score.score_home],[score.away],[score.score_away]])

这将在一行中输出所有内容。