Population dynamics 9

Reading text files

To study the numbers of different species, we will read data from the counts taken at various locations, sum them up, and store them in a new text file.

You have a text file containing population dynamics data of various species. Each entry is stored on a separate line in the following format:

“speciesName,populationName,populationName”

Assignment

Write a Python script that includes the following functionalities:

  1. Write a function read_data that takes the name of a file as input.

· The function should open the text file.

· The file should be read line by line.

· The function returns a dictionary with the population data. The species names serve as the keys in the dictionary, and the corresponding value is the sum of the population counts for that species.

  1. write a function write_population_data that writes the population data to a text file. The function should have the following input parameters:

· populations: a dictionary with population data as returned by the read_data function.

· output file: the name of the text file where the data should be written.