s=input('<请输入文件名>')
Menschen=[]
Mann=[]
Femini=[]
mRBC=[]
mWBC=[]
fRBC=[]
fWBC=[]
def compare(x):
for m in range(0,len(x)-1):
if x[-1]==x[m]:
return False
break
def about(x):
i=str(f'{x:.3f}')
return i
with open(s,mode='r',encoding='UTF-8') as f:
data=f.readlines()[1]
data=data.split('\t')
data=list(data)
if s==['zID\tGender\tRBC(10^12/L)\tWBC(10^9/L)\tHb(g/L)\tLY(10^9/L)\n']:
print("<总人次>0 <男女人数>0/0 <RBC>NA/NA <WBC>NA/NA")
else:
for m in range(0,len(data),6):
A=None
Menschen.append(data[m])
if m!=0:
A=compare(Menschen)
if A==False:
del Menschen[-1]
continue
if A==None:
if data[m+1]=='M':
Mann.append(data[m])
mRBC.append(float(data[m+2]))
mWBC.append(float(data[m+3]))
elif data[m+1]=='F':
Femini.append(data[m])
fRBC.append(float(data[m+2]))
fWBC.append(float(data[m+3]))
f=len(Femini)
m=len(Mann)
if sum(mRBC)==0:
mR='NA'
else:
mR=about(sum(mRBC)/m)
if sum(fRBC)==0:
fR='NA'
else:
fR=about(sum(fRBC)/f)
if sum(mWBC)==0:
mW='NA'
else:
mW=about(sum(mWBC)/m)
if sum(fWBC)==0:
fW='NA'
else:
fW=about(sum(fWBC)/f)
total=len(Menschen)
a='<总人次>'+format(len(data)/6,'.0f')+' '
b='<男女人数>'+str(m)+'/'+str(f)+' '
c='<RBC>'+mR+'/'+fR+' '
d='<WBC>'+mW+'/'+fW
print(a+b+c+d)
输出格式要求
test1.daz运行输出:
<请输入文件名>test1.daz
<总人次>1<男女人数>1/03.854/NA6.691/NA
<检查人次统计如下>
1次1人
<检查1次的人按编号从小到大排列如下>
Z000001
test2.daz输出:
<请输入文件名>test2.daz
<总人次>3<男女人数>1/24.689/6.0211.982/4.848
<检查人次统计如下>
1次3人
<检查1次的人按编号从小到大排列如下>
Z000001
Z000002
Z000003
test3.daz运行输出:
<请输入文件名>test3.daz
<总人次>50<男女人数>16/174.398/3.7697.249/7.551
<检查人次统计如下>
1次17人
2次15人
3次1人
<检查3次的人按编号从小到大排列如下>
Z000007
下附文件和要求
[](链接: https://pan.baidu.com/s/1QNZgqK1cMBGQSMsU17_H7g 提取码: 1234 复制这段内容后打开百度网盘手机App,操作更方便哦)
感谢解答!
我可以解决该问题。
解决方案如下:
file_name = input("请输入文件名:")
with open(file_name, 'r') as f:
content = f.readlines()
total_people = len(content)
male_count = 0
female_count = 0
for line in content:
gender = line.split(',')[1].strip()
if gender == '男':
male_count += 1
elif gender == '女':
female_count += 1
male_ratio = str(male_count) + '/' + str(male_count + female_count)
female_ratio = str(female_count) + '/' + str(male_count + female_count)
result = str(total_people) + 'XX' + str(male_ratio) + '/' + str(female_ratio) + '/NA'
check_count_dict = {}
for line in content:
check_count = line.split(',')[-1].strip()
if check_count in check_count_dict:
check_count_dict[check_count] += 1
else:
check_count_dict[check_count] = 1
result += '\n'
for key, value in check_count_dict.items():
result += '<检查' + str(key) + '次>' + str(value) + '人\n'
people_list = []
for line in content:
if line.split(',')[-1].strip() == str(key):
people_list.append(line.split(',')[0])
sorted_people_list = sorted(people_list)
for p in sorted_people_list:
result += 'Z' + p.rjust(6, '0') + '\n'
with open('result.txt', 'w') as f:
f.write(result)
完整代码如下:
file_name = input("请输入文件名:")
with open(file_name, 'r') as f:
content = f.readlines()
total_people = len(content)
male_count = 0
female_count = 0
for line in content:
gender = line.split(',')[1].strip()
if gender == '男':
male_count += 1
elif gender == '女':
female_count += 1
male_ratio = str(male_count) + '/' + str(male_count + female_count)
female_ratio = str(female_count) + '/' + str(male_count + female_count)
result = str(total_people) + 'XX' + str(male_ratio) + '/' + str(female_ratio) + '/NA'
check_count_dict = {}
for line in content:
check_count = line.split(',')[-1].strip()
if check_count in check_count_dict:
check_count_dict[check_count] += 1
else:
check_count_dict[check_count] = 1
result += '\n'
for key, value in check_count_dict.items():
result += '<检查' + str(key) + '次>' + str(value) + '人\n'
people_list = []
for line in content:
if line.split(',')[-1].strip() == str(key):
people_list.append(line.split(',')[0])
sorted_people_list = sorted(people_list)
for p in sorted_people_list:
result += 'Z' + p.rjust(6, '0') + '\n'
with open('result.txt', 'w') as f:
f.write(result)
注意,本代码只能针对文件中每行数据的格式固定的情况进行数据分析,如果文件中数据格式不固定,需要根据具体情况灵活处理。