######原文件在这
[](https://share.weiyun.com/3MLuaXO5
)
###### def Obesity_judge(BMI):
if BMI<18.5:
return("Underweight")
if 18.5<=BMI<=24.9:
return("Normal weight")
if 25<=BMI<=29.9:
return("Overweight")
if BMI>=30:
return("Obesity")
file = open("BMI data.txt", "r")
f = open("I am here.txt", "w")
a=file.readlines()
for i in a:
b = i.split()
print(b)
if b[0]=="No.":
f.write("{:6}\t{:6}\t{:6}\t{:6}\t{:6}\t{:6}\t{:6}\t{:8}\n".format('No.','sex','height','weight','SBP','DBP','BMI','Categories'))
else:
f.write("{:6}\t".format(b[0]))
weight = float(b[3])
height = float(b[2])
c = height / 100
BMI = weight / c ** 2
d = round(BMI, 1) #小数点1位
if b[1]=="1":
f.write("{:6}\t".format("Male"))
else:
f.write("{:6}\t".format("Female"))
f.write("{:6}\t".format(b[2]))
f.write("{:6}\t".format(b[3]))
BP = b[4].split("/")
f.write("{:6}\t".format(BP[0]))
f.write("{:6}\t".format(BP[1]))
f.write("{:5}\t".format(d))
f.write("{:8}\n".format(Obesity_judge(d)))
file.close()
f.close()
如何读取并计算文件中特定行中特定列的数据?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- chuifengde 2022-01-02 17:49关注
import pandas as pd from matplotlib import pyplot as plt def Obesity_judge(series): if series['BMI'] < 18.5: return "Underweight" if 18.5 <= series['BMI'] <= 24.9: return "Normal weight" if 25 <= series['BMI'] <= 29.9: return "Overweight" if series['BMI'] >= 30: return "Obesity" df = pd.read_csv(dirname + '\\BMI data.txt', sep = '\t') df['BMI'] = round(df.weight / ((df['height'] / 100) ** 2), 1) df['Categories'] = df.apply(Obesity_judge, axis = 1) dfMale = df[df['sex'] == '1']['BMI'] dfFemale = df[df['sex'] == '0']['BMI'] plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False plt.title("男女生BMI值情况") plt.xlabel('人数') plt.ylabel('BMI值') plt.plot(dfMale.values.tolist(), label='男生BMI') plt.plot(dfFemale.values.tolist(), label='女生BMI') plt.legend() plt.show()
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 华为手机相册里面的照片能够替换成自己想要的照片吗?
- ¥15 陆空双模式无人机飞控设置
- ¥15 sentaurus lithography
- ¥100 求抖音ck号 或者提ck教程
- ¥15 关于#linux#的问题:子进程1等待子进程A、B退出后退出(语言-c语言)
- ¥20 web页面如何打开Outlook 365的全球离线通讯簿功能
- ¥15 io.jsonwebtoken.security.Keys
- ¥15 急,ubuntu安装后no caching mode page found等
- ¥15 联想交换机NE2580O/NE1064TO安装SONIC
- ¥15 防火墙的混合模式配置