显示csv中的输入


with open("life-expectancy.csv") as life_expectancy:
next(life_expectancy)
output = []
years = input('Enter the year of interest: ')
for data in life_expectancy:
clean_data = data.strip()
split_data = clean_data.split(',')
entity  = split_data[0]
code = split_data[1]
year = split_data[2]
expectancy  = float(split_data[3])

## Append to the list
output.append([entity, code, year, expectancy ])
max = max(output, key=lambda x: x[3])
min = min(output, key=lambda x: x[3])
print('')
print(f'The overall max life expectancy is {max[3]:.2f} from {max[0]} in {max[2]}')
print(f'The overall max life expectancy is {min[3]:.2f} from {min[0]} in {min[2]}')
print('')
<--------- I don't know what to do for this part!----------->
print('For the year {years}}:')
print('The average life expectancy across all countries was 54.95')
print('The max life expectancy was in Norway with 73.49')
print('The min life expectancy was in Mali with 28.077')

我想做的是当用户输入一年;该输入必须显示最大、平均和最小的预期寿命,但我的问题是我不知道如何做到这一点。请帮忙!非常感谢。

您可以尝试检查用户输入是否在csv文件(列中(中,并使用"如果在"命令,然后尝试匹配信息。

if years in ....:

p.s:print("对于年份{年}}:"(在末尾有一个额外的"}">

最新更新