473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

May 2 '06 #1
5 1577
be*******@aol.com wrote:
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>

May 2 '06 #2
> 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

May 2 '06 #3

Fredrik Lundh wrote:
be*******@aol.com wrote:
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 ?)


Both your specific and general suggestions are correct. Thanks.

May 2 '06 #4
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')
May 2 '06 #5
>> Patient: "Doctor! It hurts when I press here."
Doctor: "Well don't press there"


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

--Henny Youngman

rpd

May 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Paul | last post by:
I have a text file that I am trying to display on a web page. If I cat or more the file it formats and displays fine. When it comes up in the browser it seems to loose tabs and the format gets...
1
by: red | last post by:
I have two columns of strings of various lengths. I want the second column to be straight, so I need to find the correct number of tabs between the two columns for each row.I don't mind starting...
23
by: SeaPlusPlus | last post by:
I want to convert large files of prose to xhtml and so I need a way to remove unwanted line wraps. So, I'm looking for a freebee editor that has the capability of searching for a single "carriage...
3
by: Ben | last post by:
Hi all - I am having a bit of trouble and thought maybe someone in this group could shed some light. Here's the skinny... I am creating an automated process to import a bunch of text files...
9
by: Adam Monsen | last post by:
I kindly request a code review. If this is not an appropriate place for my request, where might be? Specific questions are in the QUESTIONS section of the code. ...
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
12
by: ibloom | last post by:
I've been trying for a couple days to build a program using pyObjC on a mac, I'm stuck on this error: <string>: inconsistent use of tabs and spaces in indentation Traceback (most recent call...
17
by: Bill Cunningham | last post by:
I was wondering if someone could look this file over for me. It compiles correctly and prints the number 2 so I know fscanf is working. I am reading a text file and converting the text data...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.