search file for tabs

Tim Chase python.list at tim.thechases.com
Wed May 3 00:07:10 CEST 2006


> 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

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 at 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








More information about the Python-list mailing list