Respuesta :

Answer:

  1. import csv  
  2. with open('data.csv') as file:
  3.    records = csv.reader(file, delimiter = ",")
  4.    year = 2010
  5.    for row in records:
  6.        if (year == 2010):
  7.            print(str(year) + ": " + row[1])
  8.            previous_pop = int(row[1])
  9.            year = year + 1
  10.            print("\n")
  11.        else:
  12.            difference = abs(int(row[1]) - previous_pop)
  13.            percent = (difference / previous_pop) * 100
  14.            print(str(year) + ": " + row[1])
  15.            print("Difference: " + str(difference))
  16.            print("Percentage difference: " + str(round(percent,2)) + "%")
  17.            print("\n")
  18.            previous_pop = int(row[1])

Explanation:

Presume that there is a CSV file with 8 records. Each records have a year and population value.

Firstly, we can import the CSV module and use it to open and read the CSV file (Line 1 - 4)

Next we can use a for loop to traverse through the read data row by row (Line 6).

For the first row (year 2010), we can print out the year and population and assign the population to a variable previous_pop and increment the year by 1 (Line 9 -10).

When the year is more than 2010, we can start calculating the difference and percentage of difference of population (Line 13 -14). Then we can print out the details (Line 15 - 17) and repeat the same process in the next iteration.