Find the largest value of any file in python

0

Good night DEVs!

I'm having some difficulties in handling files in python. I have a TXT file with Float numbers.

Example:

  

numeros.txt

     

  77 23   9 10 20.26 -20.26 2

Having this data in hand, I have to go through line by line, check if the file is empty or not, if it is empty I have to find the largest value within the file and the average of them, and this is where my doubt gains strength, how can I recover this data to perform the calculations? I have tried to save in a vet, however the data becomes strings and does not accept the conversion to Float.

I'll be leaving my code below:

#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'll be grateful.

Thanks for your attention.

    
asked by anonymous 26.04.2017 / 03:11

2 answers

2

Ok, I'll put the steps to get the desired results, do not worry about details, such as if the file exists or is empty, this is with you ( This link can be added ), follow the main logic: / p>

from scipy import mean

# Load File
lf = list(open('numeros.txt','r'))

# Remove /n
lf = [s.rstrip() for s in lf]

# Convert into a list of list
lfl = [_str.split(' ') for _str in lf]

# flaten
fflaten = [float(n) for sub_list in lfl for n in sub_list]

print ('Média: ', mean(fflaten) )
print ('Vl Máximo: ', max(fflaten))
print ('Vl Minimo: ', min(fflaten))

Média:  28.1
Vl Máximo:  130.0
Vl Minimo:  -20.26

View a jupyter notebook of this code working at this link.

  

On time:   To handle exceptions of the type whether the file exists or not, See the examples in this answer .

    
26.04.2017 / 04:37
0

First, let's consider that the file can have any type of content (both numbers and texts), without a defined format. That is, there can be multiple lines and multiple values in each line, and can vary the number of numbers in them. For example, the file numeros.txt given in question:

  

numeros.txt

     

  77 23   9 10 20.26 -20.26 2

The code should correctly identify the following values: 10, 130, 20, 77, 23, 9, 10, 20.26, -20,26,

For this, we do:

import re

# busca os valores reais no arquivo numeros.txt:
numeros = [float(i) for i in re.findall(r"-?\d+\.?\d*", open("numeros.txt").read())]

# Exibe a lista de números:
print(numeros)

The output will be:

[10.0, 130.0, 20.0, 77.0, 23.0, 9.0, 10.0, 20.26, -20.26, 2.0]

The highest value is found by the max function:

>>> print("Maior valor:", max(numeros))
Maior valor: 130.0

And the mean is calculated by dividing the sum of the numbers by the quantity:

>>> print("Média:", sum(numeros)/len(numeros))
Média: 28.1

The complete code then becomes:

import re

numeros = [float(i) for i in re.findall(r"-?\d+\.?\d*", open("numeros.txt").read())]

if numeros:
    print("Maior valor:", max(numeros))
    print("Média:", sum(numeros)/len(numeros))
else:
    print("Nenhum número encontrado")

The equivalent code, only more human readable:

import re

# Expressão regular de números reais:
pattern = re.compile(r"-?\d+\.?\d*")

# Busca todos os números do arquivo:
with open("numeros.txt") as f:
    numeros = pattern.findall(f.read())

# Converte os números para float:
numeros = [float(i) for i in numeros]

if numeros:
    print("Maior valor:", max(numeros))
    print("Média:", sum(numeros)/len(numeros))
else:
    print("Nenhum número encontrado")
  

The working code can be seen here .

    
26.04.2017 / 15:40