Home > Mobile >  Reading CSV files and pulling specific data
Reading CSV files and pulling specific data

Time:09-17

Here is some sample data

Game Date HomeTeam FT HT AwayTeam
1 (Fri) 10 Aug 2018 (W32) Manchester United FC 2-1 1-0 Leicester City FC
2 (Sat) 11 Aug 2018 (W32) AFC Bournemouth 2-0 1-0 Cardiff City FC
3 (Sat) 11 Aug 2018 (W32) Fulham FC 0-2 0-1 Crystal Palace FC
  1. Based on the user input provide the total number of goals scored by a specific team throughout the season.

  2. Asks user for the game number and provide names of the both teams and score for the game.

This is what i have so far...

def t_goals():

    f = open("EPL_18-19_HW2.txt")
    next(f)
    total_goals = 0
    for lines in f:
        game = lines.strip().split(',')
        goals = game[3].split("-")
        for num in goals:
            total_goals  = int(num)


        f.close()
        return total_goals

CodePudding user response:

If you are not using pandas then use the inbuilt csv module(https://docs.python.org/3/library/csv).

Read your file like this:

def t_goals():
    with open('your_csv_file.csv', 'r') as input:
        # create a reader for your file
        input_reader = csv.reader(input, delimiter=',')

        # skip the first line which has the column names
        next(input_reader)

        total_goals = 0

        # all the lines can be read by iteration
        for line in input_reader:
            # since all the values a line in a csv file are separated by comma, they are read as a list
            # so read the FT column with line[3]
            goals = line[3].split("-")
            for num in goals:
               total_goals  = int(num)
            # if you open a file using 'with', you don't have to explicitly write the close statement
            return total goals

CodePudding user response:

Here are some quick functions I wrote that I think acheive what you want.

For the first problem check if the team you passed to the function is either home or away then get the corresponding score and add it to total_goals:

def get_total_goals(target_team='Manchester United FC'):

    total_goals = 0

    with open('sample.csv', 'r') as f:
        next(f)

        for line in f:

            current_home_team = line.strip().split(',')[2].strip()
            current_away_team = line.strip().split(',')[5].strip()

            if current_home_team == target_team:
                score = int(line.strip().split(',')[3].split('-')[0])
                total_goals  = score

            elif current_away_team == target_team:
                score = int(line.strip().split(',')[3].split('-')[1])
                total_goals  = score

        return total_goals

For the next problem, iterate through the rows and check if the game number equals the game number you've passed into the function. If there's a match, return the required details in a dictionary, if not then "Game not found" is returned.

def get_game_details(game_number=1):
    with open('sample.csv', 'r') as f:
        next(f)
        
        for line in f:
            if int(line.strip().split(',')[0]) == game_number:
                return {
                    'HomeTeam':line.strip().split(',')[2], 
                    'AwayTeam':line.strip().split(',')[5], 
                    'FT':line.strip().split(',')[3], 
                    'HT':line.strip().split(',')[4]
                }
            
        return "Game not found"

These should give you a starting point, you can make changes as required for your use case. You can also use the default csv module included in python as mentioned by anotherGatsby in their answer.

  • Related