I am writing a depth and breadth first search for a list of vertices in Python. I am trying to read in a .txt file that looks like the following:
50
60
45
12
68
21
13
24
My code reads as follows:
def readFile(x):
fin = open(x, 'r')
readline = fin.read()
x,y = [], []
for line in readline:
row = line.split()
x.append(row[0])
print(x)
y.append(row[1])
Unfortunately, when the code reads the txt file into Python this only reads the 6 into the program and terminates saying that index is out of range for y. In my .txt file there are no spaces or \n characters other than at the end of each set of points.
Any ideas on why it's adding all this additional white space and \n characters?
Side Note: When I use the sys.stdout.write(line)
the output is exactly what I am looking for, but I can't index that.
with open(x) as fin:
for line in fin:
sys.stdout.write(line)
Any help would be much appreciated!