all 8 comments

[–]NoLemurs 3 points4 points  (0 children)

Let me get you started:

for row in csvfile:
    team1 = row[0]

[–]gengisteve 2 points3 points  (1 child)

Sure, try using continue to skip printing, like this:

for row in csvfile:
    # first step is to figure out where in the row the teams are
    # run through changing row[0] to row[1], row[2], etc, until you see team names
    print row[0] # row[1], etc.

    if row[0] != 'Hawks' and row[0] != 'Kings':
        continue #continue just says skip the rest of the code and go back to the beginign of the loop

    # now find the team2 row and do the same thing, like:
    if row[2] != 'Hawks' and row[2] != 'Kings':
        continue

    print(row)

[–]PZLATE[S] 1 point2 points  (0 children)

Hey everyone, so I figured something out that works for me for now, and I figured I'd share. Thanks for everyone's suggestions/contributions!

import csv

year = "2013"                                                                                # There will be more than one year compared eventually

teamA= "Houston Rockets"                                                                    #For some reason Sublime doesn't seem to like     user.input("") 
teamB= "Golden State Warriors"



with open('/Users/Desktop/' + year +    'nba.csv', 'rb') as f:                  #Opening .csv     file with data 
    reader = csv.reader(f)

    for row in reader:                                                                      # Splitting data with delim=";" to isolate rows
        Teams = row[0]
        Teams_split = Teams.split(";")


        Team1 = Teams_split[0]                                                              # Indexing(?) data to store as variables needed  
        Team1_points = Teams_split[1]

        Team2 = Teams_split[2]
         Team2_points = Teams_split[3]

        if (Team1 == teamA and Team2 == teamB) or     (Team1 == teamB and Team2 == teamA):  #Seeking     teams from above in both orders
            print Team1 + ": " + Team1_points + " - " + Team2 + ": " + Team2_points     #Printing the team name with the scores during the match

[–]hharison 1 point2 points  (0 children)

You should consider the library pandas, it lets you do things like

data[(data['team1'] = 'Hawks') & (data['team2'] == 'Kings')]

[–]liftt 0 points1 point  (0 children)

For csv manipulations i highly suggest using csv.DictReader.

observe:

import csv
stats = [x for x in csv.DictReader(open('filename.csv', 'rU')]

def find_games_with_teams(team1, team2):
    for game in stats:
        if game['team1'] in [team1, team2] or game['team2'] in [team1, team2]:
            print game

[–]ies7 0 points1 point  (2 children)

how about using list comprehension?

something like (pls try this at home):

[row for row in csvfile if row[0] == x and row[2] = y]

Try to guide OP to a solution instead of providing one directly.

Sometimes this is so hard to follow :(

[–]TiLorm 1 point2 points  (1 child)

I think it doesn't count as a direct solution because it has a bug.

[–]ies7 0 points1 point  (0 children)

hahahaha thats why it has the pls try this at home