start reading from certain line

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • antar2

    start reading from certain line

    I am a starter in python and would like to write a program that reads
    lines starting with a line that contains a certain word.
    For example the program starts reading the program when a line is
    encountered that contains 'item 1'


    The weather is nice
    Item 1
    We will go to the seaside
    ....

    Only the lines coming after Item 1 should be read

    Thanks!
  • Diez B. Roggisch

    #2
    Re: start reading from certain line

    antar2 wrote:
    I am a starter in python and would like to write a program that reads
    lines starting with a line that contains a certain word.
    For example the program starts reading the program when a line is
    encountered that contains 'item 1'
    >
    >
    The weather is nice
    Item 1
    We will go to the seaside
    ...
    >
    Only the lines coming after Item 1 should be read
    Start reading each line, and skip them until your criterion matches. Like
    this:

    def line_skipper(pr edicate, line_iterable):
    for line in line_iterable:
    if predicate(line) :
    break
    for line in line_iterable:
    yield line

    Diez

    Comment

    • Tim Cook

      #3
      Re: start reading from certain line


      On Wed, 2008-07-09 at 03:30 -0700, antar2 wrote:
      I am a starter in python and would like to write a program that reads
      lines starting with a line that contains a certain word.
      For example the program starts reading the program when a line is
      encountered that contains 'item 1'


      The weather is nice
      Item 1
      We will go to the seaside
      ...

      Only the lines coming after Item 1 should be read
      file=open(filen ame)
      while True:
      line=file.readl ine()
      if not line:
      break

      if 'Item 1' in line:
      print line


      HTH,
      Tim


      --
      *************** *************** *************** *************** **************
      Join the OSHIP project. It is the standards based, open source
      healthcare application platform in Python.

      *************** *************** *************** *************** **************

      -----BEGIN PGP SIGNATURE-----
      Version: GnuPG v1.4.7 (GNU/Linux)

      iD8DBQBIdJgQ2TF RV0OoZwMRAoVLAJ 9GCFJDFj5oC2dnp RyscAXYo5/adACfW9Zs
      Nv1UyhWGN5c3sEp tr34bDw8=
      =IUgo
      -----END PGP SIGNATURE-----

      Comment

      • Tim Cook

        #4
        Re: start reading from certain line

        On Wed, 2008-07-09 at 03:30 -0700, antar2 wrote:
        I am a starter in python and would like to write a program that reads
        lines starting with a line that contains a certain word.
        For example the program starts reading the program when a line is
        encountered that contains 'item 1'


        The weather is nice
        Item 1
        We will go to the seaside
        ...

        Only the lines coming after Item 1 should be read
        file=open(filen ame)
        while True:
        line=file.readl ine()
        if not line:
        break

        if 'Item 1' in line:
        print line


        HTH,
        Tim


        --
        *************** *************** *************** *************** **************
        Join the OSHIP project. It is the standards based, open source
        healthcare application platform in Python.
        Home page: https://launchpad.net/oship/
        Wiki: http://www.openehr.org/wiki/display/...loper%27s+page
        *************** *************** *************** *************** **************

        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.4.7 (GNU/Linux)

        iD8DBQBIdJmO2TF RV0OoZwMRAuwcAJ 9cTcQdyiBMxr6ic tzePenLqHYd3ACg ttA4
        +s098hCCPN8N+Wo GOzNmnfs=
        =kKwH
        -----END PGP SIGNATURE-----

        Comment

        • A.T.Hofkamp

          #5
          Re: start reading from certain line

          On 2008-07-09, antar2 <desothier@yaho o.comwrote:
          I am a starter in python and would like to write a program that reads
          lines starting with a line that contains a certain word.
          For example the program starts reading the program when a line is
          encountered that contains 'item 1'
          >
          >
          The weather is nice
          Item 1
          We will go to the seaside
          ...
          >
          Only the lines coming after Item 1 should be read
          Not possible at most OSes, file reading always starts at the first character at
          the first line. Also, most OSes don't understand the 'line' concept natively, a
          file is just a long sequence of characters to them (end-of-line is also just a
          character, namely '\n' (or '\r\n' if you are using Windows).

          So you have to read the entire file, then throw away the bits you don't want to
          keep. Luckily, Python does understand what a 'line' is, which makes the prblem
          simpler.

          Have a look at the readline() function (or the readlines() function if your
          file is not too long). That should give you a start.



          Albert

          Comment

          • norseman

            #6
            Re: start reading from certain line

            Tim Cook wrote:
            On Wed, 2008-07-09 at 03:30 -0700, antar2 wrote:
            >I am a starter in python and would like to write a program that reads
            >lines starting with a line that contains a certain word.
            >For example the program starts reading the program when a line is
            >encountered that contains 'item 1'
            >>
            >>
            >The weather is nice
            >Item 1
            >We will go to the seaside
            >...
            >>
            >Only the lines coming after Item 1 should be read
            >
            file=open(filen ame)
            while True:
            line=file.readl ine()
            if not line:
            break
            >
            if 'Item 1' in line:
            print line
            >
            >
            HTH,
            Tim
            >
            >
            >
            >
            ------------------------------------------------------------------------
            >
            --
            http://mail.python.org/mailman/listinfo/python-list
            =============== =============== =============== ==========

            I would use:

            readthem= 0
            file=open(filen ame,'r')
            while readthem == 0:
            line=file.readl ine()
            if not line:
            break
            if 'Item 1' in line:
            readthem= 1
            # print line # uncomment if 'Item 1' is to be printed
            while line:
            line= file.readline()
            print line # see note-1 below
            # end of segment

            The first while has lots of needed tests causing lots of bouncing about.
            May even need more tests to make sure it is right tag point. As in if
            'Item 1' accidentally occurred in a line previous to intended one.
            The second while just buzzes on through.
            If the objective is to process from tag point on then processing code
            will be cleaner, easier to read in second while.

            note-1:
            in this form the line terminators in the file are also printed and then
            print attaches it's own EOL (newline). This gives a line double spacing
            effect, at least in Unix. Putting the comma at the end (print line,)
            will stop that. You will need to modify two lines if used. Which to use
            depends on you.


            Steve
            norseman@hughes .net




            Comment

            • Steven D'Aprano

              #7
              Re: start reading from certain line

              On Wed, 09 Jul 2008 09:59:32 -0700, norseman wrote:
              I would use:
              >
              readthem= 0
              file=open(filen ame,'r')
              while readthem == 0:
              line=file.readl ine()
              if not line:
              break
              if 'Item 1' in line:
              readthem= 1
              # print line # uncomment if 'Item 1' is to be printed
              while line:
              line= file.readline()
              print line # see note-1 below
              # end of segment

              Ouch! That's a convoluted way of doing something which is actually very
              simple. This is all you need:

              outfile = open('filename' , 'r')
              for line in outfile:
              if 'item 1' in line.lower():
              print line
              break
              for line in outfile:
              print line


              If you don't like having two loops:

              outfile = open('filename' , 'r')
              skip = True
              for line in outfile:
              if 'item 1' in line.lower():
              skip = False
              if not skip:
              print line


              And if you want an even shorter version:

              import itertools
              outfile = open('filename' , 'r')
              for line in itertools.dropw hile(
              lambda l: 'item 1' not in l.lower(), outfile):
              print line



              --
              Steven

              Comment

              • jstrick

                #8
                Re: start reading from certain line

                Here's a simple way to do it with a minimum amount of loopiness (don't
                forget to use 'try-except' or 'with' in real life):

                f = open("item1.txt ")

                for preline in f:
                if "Item 1" in preline:
                print preline,
                for goodline in f:
                # could put an end condition with a 'break' here
                print goodline,

                f.close()

                Comment

                • Iain King

                  #9
                  Re: start reading from certain line

                  On Jul 10, 2:45 pm, jstrick <jstr...@mindsp ring.comwrote:
                  Here's a simple way to do it with a minimum amount of loopiness (don't
                  forget to use 'try-except' or 'with' in real life):
                  >
                  f = open("item1.txt ")
                  >
                  for preline in f:
                      if "Item 1" in preline:
                          print preline,
                          for goodline in f:
                              # could put an end condition with a 'break' here
                              print goodline,
                  >
                  f.close()
                  No

                  Comment

                  • Iain King

                    #10
                    Re: start reading from certain line

                    On Jul 10, 4:54 pm, Iain King <iaink...@gmail .comwrote:
                    On Jul 10, 2:45 pm, jstrick <jstr...@mindsp ring.comwrote:
                    >
                    Here's a simple way to do it with a minimum amount of loopiness (don't
                    forget to use 'try-except' or 'with' in real life):
                    >
                    f = open("item1.txt ")
                    >
                    for preline in f:
                        if "Item 1" in preline:
                            print preline,
                            for goodline in f:
                                # could put an end condition with a 'break' here
                                print goodline,
                    >
                    f.close()
                    >
                    No
                    Ignore that

                    Comment

                    Working...