Getting python to read log file analysis Lecture

 https://www.bilibili.com/video/av77410524?p=29

 

ls = line.split () action is to this line of code elements of each row are separated by spaces, into a list coexist

The document read into a string reserved 

Time into the character string in txt

 

 

------------------------------------------------------------------------------------------------

The second part of the loop

 

 

 

 

try code if an error occurs, the following code will be executed except

If there is no error, then the normal program execution, except it will not execute code section

The data and calculation of avg

Cnt to calculate the number of lines of log files

avg / cnt obtain an average temperature

 

Modify the code to try to obtain the light, the average value of the voltage

#sensor_reader.py
#读取日志文件代码
try:
    f = open("sensor_data.txt","r")
	avg, count=0, 0
	voltage = 0
	light = 0
	for line in f:
	    ls = line.split()
		count += 1
		avg += ls[2]
		voltage += ls[-1]
		light += ls[-2]
    print("平均温度值是:{:.2f}".format(avg/count))
	print("平均光照是:{:.2f}".format(light/count))
	print("平均voltage是:{:.2f}".format(voltage/count))
	f.close()

except:
    print("文件打开错误")

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Published 101 original articles · won praise 73 · views 120 000 +

Guess you like

Origin blog.csdn.net/usstmiracle/article/details/104451363