read a files particular line

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

    #1

    read a files particular line

    Hi,
    does anybody have an idea how to read a particular line of a file like awk's
    awk '{if( NR==12) print $0}' filename (reads lineno 12 of filename)

    thanks for your help

    Frank Grellert
  • Kristóf Stróbl

    #2
    Re: read a files particular line

    Hi,

    If the file is not too big use:

    file("filename" ).readlines()[11]

    Kristóf

    Frank Grellert wrote:
    [color=blue]
    > Hi,
    > does anybody have an idea how to read a particular line of a file like awk's
    > awk '{if( NR==12) print $0}' filename (reads lineno 12 of filename)
    >
    > thanks for your help
    >
    > Frank Grellert[/color]

    Comment

    • Peter L Hansen

      #3
      Re: read a files particular line

      Kristóf Stróbl wrote (top-posting, but I fixed it. You're welcome.):[color=blue]
      > Frank Grellert wrote:[color=green]
      >> does anybody have an idea how to read a particular line of a file
      >> like awk's awk '{if( NR==12) print $0}' filename (reads lineno 12 of
      >> filename)[/color]
      >
      > If the file is not too big use:
      >
      > file("filename" ).readlines()[11][/color]

      And if the file is "too big", there's always iterators:

      print itertools.islic e(open('tempx.t xt'), 11, 12).next()

      A little more complicated, but avoids reading the entire file
      into memory.

      -Peter

      Comment

      • Miki Tebeka

        #4
        Re: read a files particular line

        Hello Frank,
        [color=blue]
        > Hi,
        > does anybody have an idea how to read a particular line of a file like awk's
        > awk '{if( NR==12) print $0}' filename (reads lineno 12 of filename)[/color]
        Check the documentation about the "linecache" .

        HTH.
        --
        ------------------------------------------------------------------------
        Miki Tebeka <miki.tebeka@gm ail.com>
        Find information, resources and relevant links for spymac.net. This domain may be for sale.

        The only difference between children and adults is the price of the toys

        Comment

        • Michael Hudson

          #5
          Re: read a files particular line

          Frank Grellert <fgrellert@t-online.de> writes:
          [color=blue]
          > Hi,
          > does anybody have an idea how to read a particular line of a file like awk's
          > awk '{if( NR==12) print $0}' filename (reads lineno 12 of filename)
          >[/color]

          for lineno, line in enumerate(open_ file):
          # lineno starts at 0, though

          Cheers,
          mwh

          --
          <radix> I feel so special when people quote me
          -- from Twisted.Quotes

          Comment

          • Alex Martelli

            #6
            Re: read a files particular line

            Frank Grellert <fgrellert@t-online.de> wrote:
            [color=blue]
            > does anybody have an idea how to read a particular line of a file like awk's
            > awk '{if( NR==12) print $0}' filename (reads lineno 12 of filename)[/color]

            Standard Python library module linecache, which others suggested, is
            your best bet. itertools.islic e(open('filenam e'), 12, 13).next() should
            also work (after an 'import itertools' of course, since it, too, is a
            module in the standard library) and might be faster if you'll never need
            to get any other line from that file in the course of your program.


            Alex

            Comment

            Working...