Storing lines from a text file

Discussion in 'Python' started by bradfordh@gmail.com, Jan 29, 2006.

  1. Guest

    Hello everyone.

    I am not sure how hard of a question is, but I do know that I need some
    help if you can give it. What I want to do is read the lines in a text
    file and store each line into a variable. I believe that I can use
    readlines() to read the individual lines, but how would I store each
    line into a variable for further analysis? Thanks for any and all
    suggestions.


    -Tempo-
     
    , Jan 29, 2006
    #1
    1. Advertising

  2. wrote:

    > I am not sure how hard of a question is, but I do know that I need some
    > help if you can give it. What I want to do is read the lines in a text
    > file and store each line into a variable. I believe that I can use
    > readlines() to read the individual lines, but how would I store each
    > line into a variable for further analysis? Thanks for any and all
    > suggestions.


    any reason you cannot do the analysis on the list you get from readlines ?

    if you insist on having the lines in individual variables, you can use straight-
    forward sequence unpacking:

    a, b, c, d, e = f.readlines()

    </F>
     
    Fredrik Lundh, Jan 29, 2006
    #2
    1. Advertising

  3. Guest

    so this:
    a, b, c, d, e =f.readlines()

    ...this will put the first line in a, second in b, etc? How do I
    accomplish this when I'm not sure how many lines there are going to be
    every time? Thanks.
     
    , Jan 29, 2006
    #3
  4. Kirk McDonald, Jan 29, 2006
    #4
  5. Guest

    With what kind of list? I don't see how I can do it with a list unless
    I create one indefinate list and use the objects in the indefinate list
    for the names of the lists to hold the lines of text. Is that how you
    are suggesting that I do it?
     
    , Jan 29, 2006
    #5
  6. wrote:
    > With what kind of list? I don't see how I can do it with a list unless
    > I create one indefinate list and use the objects in the indefinate list
    > for the names of the lists to hold the lines of text. Is that how you
    > are suggesting that I do it?
    >


    You're thinking too hard. Say you want to read in all the lines from the
    file object f and just print them out one at a time:

    lines = f.getlines()
    for line in lines:
    print line

    Simple.

    -Kirk McDonald
     
    Kirk McDonald, Jan 29, 2006
    #6
  7. Guest

    I keep getting an error when I try to use what you said Mr. McDonald. I
    think I've got something wrong, but take a look if you can.

    log = open('C:\log_0.txt')
    lines = log.getlines()
    for line in lines:
    print line

    When I debug it the error I get is the following:
    AttributeError: 'file' object has no attribute 'getlines'
     
    , Jan 30, 2006
    #7
  8. wrote:
    > I keep getting an error when I try to use what you said Mr. McDonald. I
    > think I've got something wrong, but take a look if you can.
    >
    > log = open('C:\log_0.txt')
    > lines = log.getlines()
    > for line in lines:
    > print line
    >
    > When I debug it the error I get is the following:
    > AttributeError: 'file' object has no attribute 'getlines'
    >


    D'oh! That's because the method is readlines(). Stupid brain:

    log = open('C:\log_0.txt')
    lines = log.readlines()
    for line in lines:
    print line

    -Kirk McDonald
     
    Kirk McDonald, Jan 30, 2006
    #8
  9. <> wrote:

    > I keep getting an error when I try to use what you said Mr. McDonald. I
    > think I've got something wrong, but take a look if you can.
    >
    > log = open('C:\log_0.txt')
    > lines = log.getlines()
    > for line in lines:
    > print line
    >
    > When I debug it the error I get is the following:
    > AttributeError: 'file' object has no attribute 'getlines'


    Yep, the method's name is readlines, not getlines.


    Alex
     
    Alex Martelli, Jan 30, 2006
    #9
  10. Guest

    Alright now that I know how to read the text file line by line, my
    question is almost answered completely. And before I ask anything else
    I want to say thanks for the help recieved already.

    Well the last thing I need help on is storing each line into one big
    list. Each line needs to have its own place in the list, not just the
    entire file into the first space of the list. For example if the list
    is titled s;
    then..
    s[0] ..would hold the first line of the text file
    s[1] ..would hold the second line of the text file
    " "
    " "
    " "
    s[n] "
     
    , Jan 30, 2006
    #10
  11. Guest

    Whoops. I figured it out. Thanks for everybody's help.
     
    , Jan 30, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Wright
    Replies:
    0
    Views:
    553
    Joe Wright
    Jul 27, 2003
  2. Replies:
    0
    Views:
    270
  3. Murali
    Replies:
    2
    Views:
    609
    Jerry Coffin
    Mar 9, 2006
  4. christrier
    Replies:
    12
    Views:
    226
    Bart Lateur
    Nov 16, 2005
  5. Cah Sableng
    Replies:
    0
    Views:
    263
    Cah Sableng
    Apr 23, 2007
Loading...

Share This Page