import csv
from numpy import *
from collections import Counter
# 'test_scan_all_cups_20230109.csv'
def txt_data(txtname,column):
'''
:param txtname: 读取的文件名称
:param column: 文件第几列信息
:return: 列中非0数据的最大值、最小值、平均值及最大值、最小值个数统计
'''
try:
data = []
with open(txtname) as f:
#读取文件
f_csv=csv.reader(f)
#获取headers
next(f_csv)
# print('headers:',headers)
for row in f_csv:
if int(row[column]) != 0:
data.append(int(row[column]))
#获取列表最大值
ma=max(data)
mi=min(data)
av=round(mean(data),2)
#字典
# Counter(data)[255]
print('Min:',mi,'(',Counter(data)[mi],')','\nMax:',ma,'(',Counter(data)[ma],')','\nAvg',av)
except Exception as ex:
print(ex)
txtname='txt_20230109.csv'
column=0
txt_data(txtname,column)
#输出结果:
Min: 99 ( 1 )
Max: 255 ( 3133 )
Avg 171.53
表格信息: