#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Need some help reading txt files


    I've made a script for python 3 with some help, but I don't understand how it works. I am trying to just read the 2nd line of a .txt file. Here it is

    in_file = open("test.txt", "rt")
    for i in range(1,3):
    text = in_file.readline()
    print (text)
    in_file.close()

    I am working on preparing for a contest, and I need to be able to read a .txt file they give me automatically. I don't understand why a for loop must be used to do this...
    Also, when I try to read the first line, there is always a empty bar at the end. I think I can manipulate it away, but it would be helpful if it was gone automatically. If theres a simpler way to do this code, please tell me.

    Thanks in advance
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    588
    Rep Power
    64
    Maybe this helps:
    Code:
    data = '''\
    john
    bob
    jim
    '''
    # write the test data to a file
    with open("test.txt", "w") as fout:
        fout.write(data)
    
    
    in_file = open("test.txt", "r")
    for i in range(1, 3):
        text = in_file.readline()
        # print only the second line
        if i == 2:
            # rstrip() removes the trailing newline char
            print("line 2 = ", text.rstrip())
    in_file.close()
    
    
    # simpler ...
    in_file = open("test.txt", "r")
    # read all lines
    lines = in_file.readlines()
    # print only the second line
    # line index starts at zero
    print("line 2 = ", lines[1].rstrip())
    in_file.close()
    
    
    # more modern
    with open("test.txt", "r") as fin:
        print("line 2 = ", fin.readlines()[1].rstrip())
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    Are you sure you want to post programs that write files into someone's file system?

    You could test that the file does not exist first rather than clobbering.

    Or use io.StringIO which admittedly is no fun because of unicode differences between python 2 and 3.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Are you sure you want to post programs that write files into someone's file system?

    You could test that the file does not exist first rather than clobbering.

    Or use io.StringIO which admittedly is no fun because of unicode differences between python 2 and 3.
    No, its for a contest
    www.dwite.ca

    Originally Posted by Dietrich
    Maybe this helps:
    Code:
    data = '''\
    john
    bob
    jim
    '''
    # write the test data to a file
    with open("test.txt", "w") as fout:
        fout.write(data)
    
    
    in_file = open("test.txt", "r")
    for i in range(1, 3):
        text = in_file.readline()
        # print only the second line
        if i == 2:
            # rstrip() removes the trailing newline char
            print("line 2 = ", text.rstrip())
    in_file.close()
    
    
    # simpler ...
    in_file = open("test.txt", "r")
    # read all lines
    lines = in_file.readlines()
    # print only the second line
    # line index starts at zero
    print("line 2 = ", lines[1].rstrip())
    in_file.close()
    
    
    # more modern
    with open("test.txt", "r") as fin:
        print("line 2 = ", fin.readlines()[1].rstrip())
    I will try it, thanks so much
    Edit: thats so cool! Where did you learn that?
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    Sorry, my post was directed toward Dietrich, who does great work but in this case I questioned posting code that writes files on someone else's system, even given the innocuous name test.txt .
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    588
    Rep Power
    64
    I usually do this in a working folder called 'Bull' or such.
    I don't want to make things so complicated that the OP can't find the meat.

    Comments on this post

    • b49P23TIvg agrees : Yes, that additional junk masks the meat.
    Last edited by Dietrich; November 25th, 2012 at 06:33 PM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo