# Write a function to open and read a file named StarWars.txt # from http://www.cs.virginia.edu/~up3f/cs1110/practice-of-the-day/StarWars.txt # # Let's practice opening a file locally and via a URL (internet). # That is, # - for the first solution, you will download the file and # save it to your machine (same folder where your .py file is) # - for the second solution, you will open the file via a URL # # The function will return 2 things (in order) # 1. The number of StarWars' fans # 2. The total number of respondents # # There are many fields for each line. You only need the following fields to complete this exercise: # RespondentID (0) # Have you seen any of the 6 films in the Star Wars franchise? (1) # -- possible value "Yes" or "No" # Do you consider yourself to be a fan of the Star Wars film franchise? (2) # -- possible value "Yes" or "No" # # Note: There are exactly 2 header lines. # ## this solution reads a file locally def count_number_fans(filename): number_respondents = 0 number_fans = 0 num_line_skip = 2 infile = open(filename, "r") for line in infile: if num_line_skip > 0: num_line_skip -= 1 else: line = line.strip().split(",") if line[2] == "Yes": number_fans += 1 number_respondents += 1 infile.close() return number_fans, number_respondents print(count_number_fans("StarWars.txt"))