I have a spreadsheet of values for subway lines and their stops. Basically, I am trying to iterate over the CSV file so that when you input a train line it spits out all the stops. I am doing this by creating a dictionary where the key is the train line and the values are a list of the stops. I am telling the function to append but when I print it only shows the last value on the sheet. Thanks for your help!
import os;
class MTALines:
def __init__(self,train="",stop_name=""):
self.__points=[];
self.train=train;
self.stop_name=stop_name;
def addLine(self,stop_name):
self.__points.append(self.stop_name);
def __repr__(self):
string=""
for item in self.__points:
string+=item+","
return string
def main():
inFile = open("hw8 - mta train stop data.csv", "r")
header = inFile.readline();
MTA={};
for line in inFile:
parts = line.split(',');
stop_id=parts[0]
train=stop_id[0]
stop_name=parts[2]
getstops = MTALines(train,stop_name);
getstops.addLine(stop_name);
MTA[train] = getstops;
trainline=""
while trainline!="done":
trainline=input("Please enter a train line, or 'done' to stop: ")
print(trainline,"line:",MTA[trainline])
main();
Update:
As requested here are a few lines of the CSV file:
stop_id,stop_code,stop_name,stop_desc,stop_lat,stop_lon,zone_id,stop_url,location_type,parent_station
101,,Van Cortlandt Park - 242 St,,40.889248,-73.898583,,,1,
101N,,Van Cortlandt Park - 242 St,,40.889248,-73.898583,,,0,101
101S,,Van Cortlandt Park - 242 St,,40.889248,-73.898583,,,0,101
209S,,Burke Av,,40.871356,-73.867164,,,0,209
210,,Allerton Av,,40.865462,-73.867352,,,1,
210N,,Allerton Av,,40.865462,-73.867352,,,0,210
210S,,Allerton Av,,40.865462,-73.867352,,,0,210
train=stop_id[0]which is essentially the first char of each line. How is this a train?