0

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!

jwodder
  • 54,758
  • 12
  • 108
  • 124
Brock Morrison
  • 99
  • 2
  • 3
  • 9

3 Answers3

0
def readFile(x):
    fin = open(x, 'r')
    readline = fin.readlines()
    x,y = [], []
    for line in readline[::2]: #to skip the extra line [before editing question]
        x.append(line[0])
        y.append(line[1])

Edited : You have to use readlines() or read().split()[both will read the content into memory ] or you can just iterate the object[this will hold only one line at a time]

You have to use strip to remove extra white characters not split

def readFile(x):
    with open(x, 'r') as fin:
        x, y = [], []
        for line in fin:
            row = line.strip()
            x.append(row[0])
            y.append(row[1])
        print(x,y)

readFile('a.txt')        

Output: ['5', '6', '4', '1', '6', '2', '1', '2'] ['0', '0', '5', '2', '8', '1', '3', '4']

Smart Manoj
  • 5,230
  • 4
  • 34
  • 59
  • Whilst this code snippet is welcome, and may provide some help, it would be [greatly improved if it included an explanation](//meta.stackexchange.com/q/114762) of *how* it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Apr 25 '17 at 14:37
0

You are close, remove readline = fin.read(); this is reading the first '6'.

then, hange for line in readline: to for line in fin:

Finally, it seems that your file has blank lines, so you have to skip them:

line = line.strip()
if line != '': 
    row = line
    x.append(row[0])
    print(x)
    y.append(row[1])
alfonso.kim
  • 2,844
  • 4
  • 32
  • 37
  • This works great for getting the first set of vertices out of the file but then the program terminates and returns x = ['50'] y = [ ]. I am looking for x = [5] y = [0] but for the entire list of vertices not just the first line. Thanks! – Brock Morrison Apr 25 '17 at 12:54
  • This works if I add spaces between my vertices in my text file, so I will just make sure that when vertices are added they have spaces in between the x and y coordinates! Thanks! – Brock Morrison Apr 25 '17 at 13:01
  • You don't need to. Because of your original post I thought that your file had empty lines. – alfonso.kim Apr 25 '17 at 13:02
  • The file itself didn't have empty lines, the empty lines were occurring when the file was being read into python. When I would build the program all the sudden the empty lines appeared, but my txt file remained without them. – Brock Morrison Apr 25 '17 at 13:13
  • That is because `readline()` takes the new line character, hence the `strip()` call is needed. I think that @Andrew Lamarra's answer will sum up what you need. – alfonso.kim Apr 25 '17 at 13:18
0

Because you don't have enough answers, here's another!

def readFile(x):
    with open(x, 'r') as fin:
        x, y = [], []
        for line in fin:
            row = line.rstrip()
            x.append(row[0])
            print(x)
            y.append(row[1])
Andrew Lamarra
  • 507
  • 2
  • 10