search file for tabs

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

    #1

    search file for tabs

    The following code to search a file for tabs does not work, at least on
    Windows XP. Could someone please tell me what's wrong? Thanks.

    xfile = "file_with_tabs .txt"
    for text in open(xfile,"r") :
    text = text.strip()
    if ("\t" in text):
    print text

  • Fredrik Lundh

    #2
    Re: search file for tabs

    beliavsky@aol.c om wrote:
    [color=blue]
    > The following code to search a file for tabs does not work, at least on
    > Windows XP. Could someone please tell me what's wrong? Thanks.
    >
    > xfile = "file_with_tabs .txt"
    > for text in open(xfile,"r") :
    > text = text.strip()
    > if ("\t" in text):
    > print text[/color]

    since you're stripping away all leading and trailing whitespace from each
    line, you'll only find lines that have tabs "in the middle".

    (your code is only five lines long. don't you think you could have double-
    checked each line a couple of times, asking yourself "what exactly is this
    line doing", in less time than it took you to compose the mail ?)

    </F>



    Comment

    • Tim Chase

      #3
      Re: search file for tabs

      > The following code to search a file for tabs does not[color=blue]
      > work, at least on Windows XP. Could someone please tell
      > me what's wrong? Thanks.
      >
      > xfile = "file_with_tabs .txt"
      > for text in open(xfile,"r") :
      > text = text.strip()
      > if ("\t" in text):
      > print text[/color]

      Well, are the tabs embedded, or at the beginning/end of the
      line? If they're at the beginning/end of the line, you're
      removing them with the strip() call.

      Solution: don't do that. :)

      Patient: "Doctor! It hurts when I press here."
      Doctor: "Well don't press there"

      tim@rubbish:~/tmp$ cat -A file_with_tabs. txt
      ^Ione^Itwo three^I $
      five^Isix $
      ^I seven^Ieight^I$
      ^Inine^Iten ^I$
      ^Ieleven^I$
      twelve^I$
      ^Ithirteen$


      With this file, and without the strip() line in your
      original, I get all the lines. With the strip, I don't get
      the "eleven" line or following. If you were using strip()
      to get rid of the newlines, you can easily enough do that with

      text = text[:-1]

      Or, depending on what your needs are, rstrip() may do the
      trick for you.

      Hope this helps,

      -tkc





      Comment

      • beliavsky@aol.com

        #4
        Re: search file for tabs


        Fredrik Lundh wrote:[color=blue]
        > beliavsky@aol.c om wrote:
        >[color=green]
        > > The following code to search a file for tabs does not work, at least on
        > > Windows XP. Could someone please tell me what's wrong? Thanks.
        > >
        > > xfile = "file_with_tabs .txt"
        > > for text in open(xfile,"r") :
        > > text = text.strip()
        > > if ("\t" in text):
        > > print text[/color]
        >
        > since you're stripping away all leading and trailing whitespace from each
        > line, you'll only find lines that have tabs "in the middle".
        >
        > (your code is only five lines long. don't you think you could have double-
        > checked each line a couple of times, asking yourself "what exactly is this
        > line doing", in less time than it took you to compose the mail ?)[/color]

        Both your specific and general suggestions are correct. Thanks.

        Comment

        • Bruno Desthuilliers

          #5
          Re: search file for tabs

          Tim Chase a écrit :
          (snip)[color=blue]
          > If you were using strip() to get rid of the newlines, you can easily
          > enough do that with
          >
          > text = text[:-1][/color]

          Which is a very bad idea IMHO.
          [color=blue]
          > Or, depending on what your needs are, rstrip() may do the trick for you.[/color]

          .... eventually with it's optional param:

          text = text.rstrip('\n ')

          Comment

          • BartlebyScrivener

            #6
            Re: search file for tabs

            >> Patient: "Doctor! It hurts when I press here."[color=blue][color=green]
            >> Doctor: "Well don't press there"[/color][/color]

            I told the doctor I broke my leg in two places.
            He told me to quit going to those places.

            --Henny Youngman

            rpd

            Comment

            Working...