编写一个车站查询系统的python课程设计代码
时间: 2024-02-04 14:03:48 浏览: 71
好的,我可以为你提供一个简单的车站查询系统的Python代码示例。这个示例并不完整,但可以帮助你了解如何使用Python编写一个简单的车站查询系统。在实际项目中,你需要根据实际需求进行详细的设计和开发。
```python
import csv
def load_stations(filename):
"""
从CSV文件中加载车站数据
"""
stations = {}
with open(filename) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
station_code = row[0]
station_name = row[1]
stations[station_code] = station_name
return stations
def find_station(stations, station_name):
"""
根据车站名称查找车站代码
"""
for code, name in stations.items():
if name == station_name:
return code
return None
def search_trains(start_station, end_station):
"""
根据起点站和终点站查询车次信息
"""
stations = load_stations('stations.csv')
start_code = find_station(stations, start_station)
end_code = find_station(stations, end_station)
if start_code is None or end_code is None:
return []
trains = []
with open('trains.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
train_code = row[0]
start = row[1]
end = row[2]
if start == start_code and end == end_code:
trains.append(train_code)
return trains
# 示例用法
start_station = '北京'
end_station = '上海'
trains = search_trains(start_station, end_station)
print(f"从{start_station}到{end_station}共有{len(trains)}趟列车:{trains}")
```
在这个示例中,我们使用了Python内置的csv模块来读取车站数据和车次数据。load_stations函数从stations.csv文件中加载所有车站数据,find_station函数根据车站名称查找车站代码,search_trains函数根据起点站和终点站查询所有符合条件的车次信息。你需要在本地创建stations.csv和trains.csv文件,分别存储车站数据和车次数据。
阅读全文