In: Computer Science
You are given a text file that contains the timetable for buses that travel a college campus. The first line of the file contains the name for each stop on the bus system separated by colons. Each following line contains the times using a 24-hour clock at which each bus in the system will arrive at a bus stop, also separated by colons.The timetable will have the following format:
Edinburgh:Danderhall:Dalkeith:Edgehead:Pathhead:Blackshiels:Oxton:Carfraemill:Lauder:Earlston:Leaderfoot:Newtown St Boswells:St Boswells:Clintmains:Kelso
0850:0911:0918:0930:0933:0939:0953:0955:1001:1015:1025:1029:1032:1038:1055
1150:1211:1218:1230:1233:1239:1253:1255:1301:1315:1325:1329:1332:1338:1355
1350:1411:1418:1430:1433:1439:1453:1455:1501:1515:1525:1529:1532:1538:1555
1610:1633:1640:1652:1655:1701:1715:1717:1723:1737:1746:1750:1753:1803:1820
1750:1811:1818:1830:1833:1839:1853:1855:1901:1919:1925:1929:1932:1938:1955
2000:2021:2028:2037:2040:2046:2100:2102:2108:2121:2126:2130:2133:2138:2155
Write a program in Python that reads this file and outputs a row for each bus stop, showing the times when a bus arrives at that stop
CODE:
file = open('data.txt','r')
#reading the lines in the file
lines = [x.split('\n')[0] for x in file.readlines()]
busStops = {}
#storing all the busStop names as keys of a dictionary
for i in lines[0].split(':'):
#each key has a list of times a bus will arrive at that particular
stop
busStops[i] = []
#for each key value or busStop
for k,j in enumerate(busStops.keys()):
for i in range(1,len(lines)):
#the kth time value on each line is the time to be stored in that
key's list
busStops[j].append(lines[i].split(':')[k])
#printing all the details
for i in busStops.keys():
print('Stop Name: ',i,'\nTimings for buses at this stop are(in 24
hrs format):')
for j in busStops[i]:
print(j,end = ' ')
print('\n')
_______________________________________________
CODE IMAGES AND OUTPUT:
______________________________________________
data.txt
____________________________________
Feel free to ask any questions in the comments section
Thank You!