0
Can anyone help me with this python file exercise?
Consider that each year there is a text file with information about the population world. Each line has the country, population, population growth rate in the previous year, fertility rate and age group, the values being a period and two points (;).
For example, suppose the file with the 2016 data is called pop2016.txt, whose first three lines are:
China;1403500365;0,0046;1,61;37,3
Angola;28813463;0,0342;5,88,16,5
Portugal;10371627;-0,0045;1,27;44,4
It is intended to implement a program that, with the name of one of these archives, a country, a number of years and the name of a second file, write in the second file the estimate of the population of that country in the years following the mentioned in the assuming that the growth rate remains constant.
If the program receives the string 'pop2016.txt', the string 'Angola', the number 3 and the string 'Angola2016-3.txt', write in the file 'angola2016-3.txt' the following three lines:
Year 1: 29798883
Year 2: 30818005
Year 3: 31871981
To understand how the population estimate is made, note that, according to Pop2016.txt, (in 2016) Angola has 28 813 463 inhabitants and the growth rate is 0.0342. Therefore, if the growth rate continues, Angola will have:
round ((1 0,0342) 28 813 463) = 29 798 883 inhabitants in 2017 (2016+1);
round ((1 0,0342) 29 798 883) = 30 818 005 inhabitants in 2018 (2016+2);
round ((1 0,0342) 30 818 005) = 31 871 981 inhabitants in 2019 (2016+3).
To resolve this exercise first I am trying to make a program that reads the file in filename and returns the information concerning the country indicated in parents
def leFich(nomeFich,pais):
fich = open(nomeFich)
a = 0
text = fich.readlines()
fich.close()
for linha in text:
linha = linha.split(';')
if str(linha[1]) == pais:
a = text[1]
return a
By calling leFich('pop2016.txt','Angola')
just appears to me
0, which is not the requested result. How do I solve this problem?
"read the file in filename and return the information concerning the country indicated in country" You want to return the entire country line?
– Gabriel
yes, that’s what I want
– stream