python-archieve-projects/M302/dataAnalyzer.py

78 lines
2.4 KiB
Python

from collections import Counter
def getRecords(filename) -> []:
try:
persons = [[]]
with open(filename, 'r', encoding='utf-8') as f:
lines = f.readlines()
index = 0
persons_text = lines[1].split('\t')
for i in range(len(persons_text) - 1):
if persons_text[i+1].startswith('Z'):
persons[index].append(persons_text[i])
index += 1
persons.append([])
else:
persons[index].append(persons_text[i])
return persons
except IOError as e:
print(e, '\nPlease try again')
def analyzeRecords(records):
print('<总人次>', len(records), end=" ")
times_stat = {}
male = 0
female = 0
male_RBC_total = 0
male_WBC_total = 0
female_RBC_total = 0
female_WBC_total = 0
for record in records:
zID = record[0]
gender = record[1]
RBC = eval(record[2])
WBC = eval(record[3])
#Hb = record[4]
#LY = record[5]
# 统计次数
if zID in times_stat:
times_stat[zID] += 1
else:
times_stat[zID] = 1
# 统计男女
if gender == '':
male += 1
male_RBC_total += RBC
male_WBC_total += WBC
else:
female += 1
female_RBC_total += RBC
female_WBC_total += WBC
print('<男女人数>', male, '/', female, end=" ")
print('<RBC>{:0.3f}/{:1.3f}'.format(male_RBC_total/male, female_RBC_total/female), end=" ")
print('<WBC>{:0.3f}/{:1.3f}'.format(male_WBC_total/male, female_WBC_total/female))
# 统计人数
counter = Counter(times_stat.values())
print("<检查人次统计如下>")
max_times = max(counter.items())[0]
for value, count in sorted(counter.items(),key=lambda x:x[0]):
print(f'{value}{count}')
# 找到次数为最高次的
max_times_lst = []
for person in times_stat:
if times_stat[person] == max_times:
max_times_lst.append(person)
# 排序 sorted默认升序
max_times_lst = sorted(max_times_lst, key=lambda x:x[0])
print('<检查', max_times, '次的人编号从小到大排列如下>')
for zID in max_times_lst:
print(zID)
if __name__ == '__main__':
filename = input("<请输入文件名>")
records = getRecords(filename)
analyzeRecords(records)