skip item in list "for loop"

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

    #1

    skip item in list "for loop"

    I am new to python and I love it. I am hacking a file. I want to not
    print a line if it contains the word 'pmos4_highv'. I also don't want
    to print the next line. The following code works but it "smells bad"
    and just doesn't look right. I was reading about generators. If I was
    using one, I could do a .next() after the match and get rid of the
    flags. Any suggestions how I can improve this?


    inPmos = False

    inFile = sys.stdin
    fileList = inFile.readline s()

    for line in fileList:
    line = line.rstrip()
    # Remove PMOS definitions - typically two lines. Screwed if only
    one and last inst.
    if inPmos:
    inPmos = False
    continue
    if 'pmos4_highv' in line:
    inPmos = True
    continue


    jr

  • Diez B. Roggisch

    #2
    Re: skip item in list "for loop"

    Jacob Rael schrieb:[color=blue]
    > I am new to python and I love it. I am hacking a file. I want to not
    > print a line if it contains the word 'pmos4_highv'. I also don't want
    > to print the next line. The following code works but it "smells bad"
    > and just doesn't look right. I was reading about generators. If I was
    > using one, I could do a .next() after the match and get rid of the
    > flags. Any suggestions how I can improve this?[/color]

    A generator certainly would be handy:

    def read_lines(inFi le):
    fg = iter(inFile)
    for line in fg:
    if "pmos4_high v" in line:
    fg.next()
    else:
    yield line



    lines = """a
    b
    c
    d
    pmos4_highv
    no no!
    e
    f
    g""".split("\n" )


    for line in read_lines(line s):
    print line


    Diez

    Comment

    • Felipe Almeida Lessa

      #3
      Re: skip item in list "for loop"

      Em Sex, 2006-04-14 às 20:33 +0200, Diez B. Roggisch escreveu:[color=blue]
      > def read_lines(inFi le):
      > fg = iter(inFile)
      > for line in fg:
      > if "pmos4_high v" in line:
      > fg.next()
      > else:
      > yield line[/color]

      Just be aware that the "fb.next()" line can raise an StopIteration
      exception that would otherwise be caught by the for loop. If you have
      any resources that need to be cleaned up, try...finally is you friend.

      --
      Felipe.

      Comment

      • Diez B. Roggisch

        #4
        Re: skip item in list "for loop"

        Felipe Almeida Lessa schrieb:[color=blue]
        > Em Sex, 2006-04-14 às 20:33 +0200, Diez B. Roggisch escreveu:[color=green]
        >> def read_lines(inFi le):
        >> fg = iter(inFile)
        >> for line in fg:
        >> if "pmos4_high v" in line:
        >> fg.next()
        >> else:
        >> yield line[/color]
        >
        > Just be aware that the "fb.next()" line can raise an StopIteration
        > exception that would otherwise be caught by the for loop. If you have
        > any resources that need to be cleaned up, try...finally is you friend.[/color]

        Actually I did that on purpose - as the StopIteration would simply end
        the generator. Any clean-up - well, if that was the scope of the
        generator on could add it of course, but I'd do that on the same level
        the file has been opened.

        Regards,

        Diez

        Comment

        • Jacob Rael

          #5
          Re: skip item in list "for loop"

          Thanks for the suggestions.
          The solution I liked most was to prevent the lines from appearing in
          the first place!!

          Comment

          Working...