import xlwt
import codecs
def compute_temp(inputfile):
f = open(r'C://Users/lenovo/Desktop/tmp/test_file.txt')
line = f.readline()
value = 0
n = 0
while line:
line = line.strip('\n')
line = line.strip(';')
list = []
list1 = []
list2 = []
list=line.split(',')
if list[0]=='T1':
list1 = list
value += int(list1[2])
date = list1[1]
n = n+1
else:
list2 = list
temp = value/n
line = f.readline()
f.close()
return [temp,date];
def temp_issue(inputfile,temp):
f = open(r'C://Users/lenovo/Desktop/tmp/test_file.txt')
line = f.readline()
value = 0
n = 0
while line:
line = line.strip('\n')
line = line.strip(';')
list = []
list1 = []
list2 = []
list=line.split(',')
if list[0]=='T1':
list1 = list
if float(list1[2])-temp > 3:
print('%s; 温度过高' %','.join(list1))
elif float(list1[2]) - temp < -3:
print('%s; 温度过低' % ','.join(list1))
else:
list2 = list
line = f.readline()
f.close()
if __name__ == '__main__':
sheetName = 'Sheet1' # 需要写入excel中的Sheet2中,可以自己设定
start_row = 1 # 从第1行开始写
start_col = 1 # 从第1列开始写
inputfile = 'C://Users/lenovo/Desktop/tmp/test_file.txt' # 输入文件
outputExcel = 'C://Users/lenovo/Desktop/tmp/excel_result.xls' # 输出excel文件
print('异常检测结果:')
result = compute_temp(inputfile)
temp=round(result[0],2)
date=result[1]
temp_issue(inputfile,temp)
print('报表结果:')
print('温度: %s :%.2f'%(date,temp))
时间问题,只处理了一部分,第二部分可通过将元素添加到字典中实现。