0
Good night Devs!
I am having certain difficulties in manipulating python files.
I have a TXT file with float numbers.
Example:
numbers.txt
10 130 20
77 23
9 10 20.26 -20.26 2
Having this data in hand, I have to go down line by line, check whether the file is empty or not, if it’s empty I have to find the highest value inside the archive and their average, and this is where my doubt gains strength, How can I recover this data to perform the calculations? i tried to save to a vet, but the data becomes strings and do not accept the conversion to Float.
I’ll be dropping my code:
#Ler o nome do arquivo
#Abrir o arquivo caso exista
#Verificar o Maior valor possivel
#Verificar a Média dos valores
#Imprimir média e Valores
def pulaLinha ():
print('\n')
arq = open("numeros.txt", "r") #ABRE O ARQUIVO TXT
########################################
#CALCULAR O TOTAL DE LINHAS DO ARQUIVO|#
########################################
text = arq.readlines()# GUARDA TODOS OS DADOS DO ARQUIVO
tot = 0 #TOTAL DE LINHAS
for i in text: #CALCULA O TOTAL DE LINHA DO ARQUIVO
tot = tot+1 #SALVA O TOTAL EM UM ACUMULADOR
########################################
#VERIFICAR SE O ARQUIVO É VAZIO OU NÃO #
########################################
if (tot == 0):#SE TOT DE LINHAS FOR IGUAL A 0
print("ARQUIVO VAZIO! ") #IMPRIME O ERRO
arq.close() # FECHA ARQUIVO
exit()
#SE O TOT DE LINHAS FOR MAIOR QUE 0
for linha in text:#ARMAZENA OS VALORES EM LINHA
print(linha, end=" ") #IMPRIME OS VALORES DE LINHA // IMPRIME O ARQUIVO
vet = []
for j in text:
vet.append(j)
arq.close()#FECHA O ARQUIVO TXT
If anyone can help me I’d be grateful.
Thank you for your attention.
Is it a number per line? You have one line
9 10 20.26 -20.26 2
what’s supposed to be that?– Miguel
@Miguel- There is a file, this file has lines, I want to find the highest value inside the file, it can be anywhere, and also find the average values.
– Félix
According to your text, it’s only one number per line, but the contents of the file you posted say otherwise. That’s what Miguel asked: will it be just one number per line or can there be more than one on the same line?
– Woss