Issue in question Python String/File Manipulation

Asked

Viewed 95 times

1

I am new to the site and I am looking for a solution to my problem. I am currently studying Python 3 and when trying to solve this issue I get an error image, I will put here my code until the current moment and the error message I receive.

soma=0
media=0
while True:
    arq1=open('CADASTRO.txt','r')
    arq = arq1.readline()
    frase=arq.split(',')
    frasefinal=frase[2].split('\n')
    arq1.close()
    if (arq == ''):
        break
    if (arq != ''):
        s=int(frasefinal[0])
        soma= s + soma
        media+=1

Traceback (Most recent call last): File "C:/List 1 file/questao9.py", line 5, in Arq = arq1.readline() File "C:## Headers ## Appdata Local Programs Python Python37-32 lib encodings cp1252.py", line 22, in Decode def Decode(self, input, final=False): Keyboardinterrupt

In case I’m trying to calculate the age average

Example of line I use in 'REGISTER.txt'

Natural,male,19

  • Could you put the error message by full? What if you open and close the file every time inside the while you will not always read the first line of the file endlessly?

3 answers

2

The while True is infinite and with each iteration, it opens the file and reads its first line. It would only stop if the first line was blank because of the break. I don’t think that’s what you want. I think what you really want is to read the file line by line.

In addition, it is good practice to read the file using the with, to ensure that it is always closed in an orderly manner, even if an error occurs in your application, without you having to call close() explicitly.

soma = 0
media = 0
with open('CADASTRO.txt', 'r', encoding='utf-8') as arq:
    while True:
        linha = arq.readline()
        if linha == '': break
        frase = linha.split(',')
        frasefinal = frase[2].split('\n')
        s = int(frasefinal[0])
        soma = s + soma
        media += 1

And absorbing the suggestion of reply from Louhan Vargas, use the UTF-8 encoding.

1

The way I see it, this is looking like an encoding problem in your file. Try adding the utf-8 encoding parameter to your line 4. Thus remaining:

arq1=open('CADASTRO.txt','r', encoding='utf-8')

If you can show the txt file data it would be better ;)

0

I don’t quite understand what you want to do, but the code I’ll send averages normally and you’ll only have to adapt with infinite loops (I think while True is not your best option), look for something to read each line of the file (while True does not)

arq1 = open('CADASTRO.txt','r')
arq = arq1.readline()
frase = arq.split(',')
idade = int(frase[2])
if arq != '':
    soma += idade
    cont += 1

and to calculate the average creates this variable

media = soma / cont
  • 1

    Why the while True would not be the best option to set an infinite loop?

  • I don’t know if that’s what you want, but you’d probably have a file with multiple criminal records, right? If so, you should go through and do the procedure until the file is finished and not infinitely, so you should make a way to read how many lines there are in the file and do a For in the range of that number, if you need something infinite really does with while True but if it is what I described look for it.

Browser other questions tagged

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