#1
  1. A wanna-be guru of some sort
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Either online or offline
    Posts
    625
    Rep Power
    19

    Arrow Reading ints from a file and assigning them


    Hi, guys!

    I have a problem that I just do not seem to see the solution to.
    I have a file that contains integers in two columns, like this:
    1 2
    2 4
    6 233
    23 2777

    I want to read the file line by line and every time I read a line, I want to
    assign the first int to a variable and the second int to another variable.
    So, the first time say, x = 1, y = 2, and finally x = 23. y = 2777. Every time I go through the loop, I want to do something with current values of x and y.
    So what I had was this:
    Code:
    while 1: 
    	line = inFile.readline().split()
    	n, m = int(line[0]), int(line[1])
    	#do stuff with m and n
    But it keeps giving me list index out of bounds error.
    However, when I do this:
    Code:
    while 1: 
    	line = inFile.readline().split()
    	print line[0]
    	print line[1]
    it prints the numbers correctly.
    I really do not see what I am doing wrong.
    I want n = 1, m = 2 the first time through the loop and n = 23, m = 2777 the last time through the loop.
    Any help would be greatly appreciated.
    Am I supposed to sign here?
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,616
    Rep Power
    4247
    I'm guessing it burps at the end of file. The reason being that on hitting eof, readline() will return None which won't split() and hence, line[0] and line[1] won't exist. You could try checking if the readline() succeeded or not.
    Code:
    while 1:
        line = inFile.readline().split()
        if not line: break
        ...rest of stuff....
    Another way is to do this would be:
    Code:
    #!/bin/env python       
    import string
    
    file = open('testpy.txt', 'r')
    for line in file:
        cols = string.split(line)
        print cols[0], cols[1]
    
    file.close()
    Of course, you
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    Originally Posted by Boki
    Hi, guys!

    I have a problem that I just do not seem to see the solution to.
    I have a file that contains integers in two columns, like this:
    1 2
    2 4
    6 233
    23 2777

    I want to read the file line by line and every time I read a line, I want to
    assign the first int to a variable and the second int to another variable.
    You need to remove whitespace from the line...
    Code:
    for line in open("myfile.txt"):
        line = line.strip()
        if not line:
            continue
        m, n = line.split()
        [... do something]
    --OH.
  6. #4
  7. A wanna-be guru of some sort
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Either online or offline
    Posts
    625
    Rep Power
    19

    Thumbs up Thanks!


    Thanks, Scorpions! Your second suggestion worked perfectly!
    Am I supposed to sign here?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    49
    Rep Power
    0
    Originally Posted by Boki
    Thanks, Scorpions! Your second suggestion worked perfectly!
    Hi!

    Even i have the Same doubt,

    I have to read a Text File

    ITEM1 100
    ITEM2 200
    ITEM3 333

    Where should i save the file, and how can i invoke this file in my python code,

    What if this file is in some other directory,

    Help me,

    thanz
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    49
    Rep Power
    0
    Originally Posted by shaharpan
    Hi!

    Even i have the Same doubt,

    I have to read a Text File

    ITEM1 100
    ITEM2 200
    ITEM3 333

    Where should i save the file, and how can i invoke this file in my python code,

    What if this file is in some other directory,

    Help me,

    thanz
    I ran this code

    #!C:\Python23\Python.exe
    >>> for line in open("abc.txt"):
    line=line.strip()
    if not line:
    continue
    m,n=line.slip()
    print m



    Traceback (most recent call last):
    File "<pyshell#17>", line 1, in -toplevel-
    for line in open("abc.txt"):
    IOError: [Errno 2] No such file or directory: 'abc.txt'
  12. #7
  13. A wanna-be guru of some sort
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Either online or offline
    Posts
    625
    Rep Power
    19
    If you're doing it from the command line, make sure you are in the directory that contains the file before you invoke the interpreter.
    Am I supposed to sign here?
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Just a few things I've noted, you should never over ride the name of a built in function in this case file().

    Also in the code above, why not do if line: ... rather than if not line: continue.

    Code:
    for line in file("/path/to/myfile.txt"):
        line = line.strip()
        if line:
            col1, col2 = line.split(' ', 1) #stops unpacking errors should the file contain more than 1 column.
        ...
    Note: the path given to the file() function, this tells Python where the file is located on the system. This can be an absolute path or be relative to your script.

    You could also incorporate this into the while loop although I would recommend against using a while loop to read a files contents; use for instead.

    Take care,

    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo