doubt text files in python

Asked

Viewed 48 times

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?

  • yes, that’s what I want

1 answer

1


I am trying to make a program that reads the file in filename and returns the information concerning the country indicated in parents

  1. Generally use underlines to name variables in Python. I recommend that you use this pattern.
  2. Whenever possible, use the with to handle files, so Python itself calls the close automatically.
  3. Indexes begin in 0, not in 1, then that your if should be linha[0] and not linha[1].
  4. You don’t need the variable a, and, besides, this is a bad name for the variable because it is not descriptive.
def ler_ficheiro(caminho_arquivo, pais):
    with open(caminho_arquivo) as arquivo:
        linhas = arquivo.readlines()

    for linha in linhas:
        campos = linha.split(';')

        if campos[0] == pais:
            return linha

    raise Exception(f'{pais} não foi encontrado em {caminho_arquivo}')

References:

  • Thank you, it was a distraction to have put line[1]. But regarding your code why it shouldn’t be str(line[0]) == parents, since parents will be a string?

  • @stream you don’t need to turn linha[0] in a string because that is already a string.

  • Okay, thanks I get it

Browser other questions tagged

You are not signed in. Login or sign up in order to post.