search file for tabs

B

beliavsky

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
 
F

Fredrik Lundh

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

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

Tim Chase

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@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
 
B

beliavsky

Fredrik said:
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 ?)

Both your specific and general suggestions are correct. Thanks.
 
B

BartlebyScrivener

Patient: "Doctor! It hurts when I press here."
I told the doctor I broke my leg in two places.
He told me to quit going to those places.

--Henny Youngman

rpd
 
B

Bruno Desthuilliers

Tim Chase a écrit :
(snip)
If you were using strip() to get rid of the newlines, you can easily
enough do that with

text = text[:-1]

Which is a very bad idea IMHO.
Or, depending on what your needs are, rstrip() may do the trick for you.

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

text = text.rstrip('\n')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top