I have a Python script that reads data from a file and processes it. I need it to calculate number of lines, prints the given measurements, prints the greatest measurement with the id where it is from and calculates the average. I have only managed to get this far:
with open(r"file.txt", 'r', encoding="UTF-8") as fp: #reads file
lines = len(fp.readlines())
print('number of lines:', lines) #prints the number of lines
with open(r"file.txt", "r") as fp:
measurements = fp.readlines() #stores lines in a list
measurements = [i.strip().split(' ') for i in measurements] #strips whitespaces, splits according to spaces
print(measurements)
data = [i[3] for i in measurements] #takes the 4th thing in line, stores it as a string the temps list
print('measured data:') #prints temps
for i in data:
print(i)`
i tired various methods, but there seems to be a fault in my logic, so pls
powerispower is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
i believe this is what u are looking for:
makes strings into floats by replacing the comma with a dot
then prints it again for verification for i in range(len(data)):
data[i] = float(data[i].replace(',', '.')) print(measurements)
sorts the list and prints the last element – the greatest one
the id corresponding to the highest measurement is found using index and printed naj_teplota = sorted(data)[-1] print(‘1measurement:’,
naj_teplota)
finds the ID where the max is print(‘measurement id:’, measurements[data.index(naj_teplota)][0])
average priemer = sum(data) / len(data) print(‘average’, priemer)
powerispower is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.