Python NFL幻想CSV

时间:2017-08-25 17:45:56

标签: python csv

请原谅我的无知,但我很难让Python做我想做的事情。所以基本上,我有一个包含国家橄榄球联盟(NFL)数据的CSV文件,其中包括四行,即位置,玩家名称,薪水和平均幻想点数(AFPG)。因此,例如在CSV文件中它的样子将是Position(RB),PlayerName(Le' Veon Bell)Salary(9800)AvgFantasyPoints(28.6)。我想做的是选择平均至少14 AFPG且低于7000薪水的PlayersName。这是我到目前为止的代码。感谢任何帮助。

import csv

out=open("NFL.csv", "rb")
reader = csv.reader(out, delimiter = ",")
data = csv.reader(out)
data = [row for row in data]

for row  in data:
    Position = row[0]
    Name = row[1]
    Salary = row[2]
    Game_info = row[3]
    Avg_points = row[4]
    Players = (Name)
    for field in Salary:
        print(if Salary <= 7000)

out.close()

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

import csv

data = csv.DictReader(open('filename.csv'))

players = [i['Name'] for i in data if i['Salary'] < 7000 and i['Ave_points'] >= 14]

如果您在Position,Name,Salary,Game_pts,Ave_pts表单中没有文件标题,则可以使用collections.namedtuple

from collections import namedtuple

stats = namedtuple("stats", "position, name, salary, gamepts, avepts")

data = map(stats._make, csv.reader(open("filename.txt")))

new_data = [i.name for i in data if i.salary > 7000 and i.avepts >= 14]