编写python程序,打开一个存储国际航班的旅客信息的CVS文件,统计该航班50岁以上男(含50))、女乘客的人数以及年龄最大的乘客姓名、年龄和座位
时间: 2024-03-10 20:47:53 浏览: 18
可以使用Python内置的csv模块来读取和处理CSV文件。以下是一个示例程序:
```python
import csv
filename = 'flight_info.csv'
male_count = 0
female_count = 0
max_age = 0
max_age_name = ''
max_age_seat = ''
with open(filename, 'r') as file:
reader = csv.reader(file)
for row in reader:
age = int(row[2])
gender = row[3]
if age >= 50:
if gender == 'M':
male_count += 1
elif gender == 'F':
female_count += 1
if age > max_age:
max_age = age
max_age_name = row[0]
max_age_seat = row[1]
print(f"Male passengers aged 50 or above: {male_count}")
print(f"Female passengers aged 50 or above: {female_count}")
print(f"Oldest passenger: {max_age_name}, age {max_age}, seat {max_age_seat}")
```
假设CSV文件的格式如下:
```
Name,Seat,Age,Gender
John Smith,12A,45,M
Mary Johnson,24B,51,F
David Lee,34C,65,M
...
```
代码首先打开CSV文件,使用csv.reader()函数读取每一行数据。然后,它检查每个乘客的年龄和性别,如果乘客的年龄大于或等于50岁,则根据性别进行计数。最后,它找出年龄最大的乘客的姓名、年龄和座位,并将它们打印出来。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)