程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python sensor acquisition data file analysis and processing experimental source code

編輯:Python

A few days ago, I found a giant artificial intelligence learning e-book,通俗易懂,風趣幽默,無廣告,忍不住分享一下給大家.(點擊跳轉人工智能學習資料)


一、題目

附件sensor-data.txtis a sensor acquisition data file,其中,每行是一個讀數,空格分隔多個含義,分別包括日期、時間、溫度、濕度、光照和電壓.其中,光照處於第5列.請編寫程序,統計並輸出傳感器采集數據中光照部分的最大值、最小值和平均值,所有值保留小數點後2位.

二、題目分析

打開txt文件可以發現,Each row has a string containing the date、時間、溫度、Humidity light and voltage data,用空格隔開,We can use a two-dimensional list,每次讀取一行,Separate by spaces,into a one-dimensional list,Then put the one-dimensional list into a two-dimensional list.After traversing the file,We've already stored all the data in a two-dimensional list,Next, iterate over this two-dimensional list,先定義一個max=0,min=10000,Then if you encounter a ratio when traversingmaxbig number,就更新max,如果遇到比min小的數,就更新min,另外我們用sumAdd all the data,And record the number of data,最後只需要輸出max和minYou can get the maximum and minimum values,輸出sum/cnt就可以得到平均值,我們可以通過%.2fControl retains two decimal places.最後記得要關閉文件!

In fact, there is another way to do this,Pass these list data throughnumpy,然後通過numpy中的max,min和meanThe function gets the maximum, minimum and average values.

三、源碼

list=[[]]
max =avg =cnt=0
min=10000
with open("sensor-data.txt","r")as file:
for line in file.readlines():
line=line.strip('\n')
list.append(line.split(" "))
list.remove([])
for templist in list:
print(type(templist))
if max<float(templist[4]):
max=float(templist[4])
if min>float(templist[4]):
min=float(templist[4])
avg+=float(templist[4])
cnt+=1
print(max)
print(min)
print("%.2f"%(avg/cnt))
file.close()

四、實驗結果


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved