Storing lines from a text file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bradfordh@gmail.com

    #1

    Storing lines from a text file

    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-

  • Fredrik Lundh

    #2
    Re: Storing lines from a text file

    bradfordh@gmail .com wrote:
    [color=blue]
    > 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.[/color]

    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>



    Comment

    • bradfordh@gmail.com

      #3
      Re: Storing lines from a text file

      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.

      Comment

      • Kirk McDonald

        #4
        Re: Storing lines from a text file

        bradfordh@gmail .com wrote:[color=blue]
        > 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.
        >[/color]

        With a list:


        -Kirk McDonald

        Comment

        • bradfordh@gmail.com

          #5
          Re: Storing lines from a text file

          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?

          Comment

          • Kirk McDonald

            #6
            Re: Storing lines from a text file

            bradfordh@gmail .com wrote:[color=blue]
            > 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?
            >[/color]

            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

            Comment

            • bradfordh@gmail.com

              #7
              Re: Storing lines from a text file

              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'

              Comment

              • Kirk McDonald

                #8
                Re: Storing lines from a text file

                bradfordh@gmail .com wrote:[color=blue]
                > 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'
                >[/color]

                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

                Comment

                • Alex Martelli

                  #9
                  Re: Storing lines from a text file

                  <bradfordh@gmai l.com> wrote:
                  [color=blue]
                  > 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'[/color]

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


                  Alex

                  Comment

                  • bradfordh@gmail.com

                    #10
                    Re: Storing lines from a text file

                    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] "

                    Comment

                    • bradfordh@gmail.com

                      #11
                      Re: Storing lines from a text file

                      Whoops. I figured it out. Thanks for everybody's help.

                      Comment

                      Working...