473,396 Members | 1,840 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,396 software developers and data experts.

stripping spaces in front of line

hi
wish to ask a qns on strip
i wish to strip all spaces in front of a line (in text file)

f = open("textfile","rU")
while (1):
line = f.readline().strip()
if line == '':
break
print line
f.close()

in "textfile", i added some spaces in and then ran the code, it prints
out the lines without the spaces in front. I double checked "textfile"
and it does contains some lines with spaces in front.
Is it true that "readline().strip()" perform the removing of spaces in
front of a line as well? Is it documented anywhere?
I am using Windows environment. thanks

Mar 4 '06 #1
6 2649
ei***********@yahoo.com wrote:
hi
wish to ask a qns on strip
i wish to strip all spaces in front of a line (in text file)

f = open("textfile","rU")
while (1):
line = f.readline().strip()
if line == '':
break
print line
f.close()

in "textfile", i added some spaces in and then ran the code, it prints
out the lines without the spaces in front. I double checked "textfile"
and it does contains some lines with spaces in front.
Is it true that "readline().strip()" perform the removing of spaces in
front of a line as well? Is it documented anywhere?
I am using Windows environment. thanks


You have observed the expected behavior. If you only want the trailing
spaces stripped, try "rstrip". If you only want the leading spaces
stripped, try "lstrip". If you want no space anywhere try this:

line = "".join(f.readline().split())

If you want to normalize space, do this:

line = " ".join(f.readline().split())

This should fulfill 90% of your space-transforming requirements.

James
Mar 4 '06 #2
> wish to ask a qns on strip
i wish to strip all spaces in front of a line (in text file)

f = open("textfile","rU")
while (1):
line = f.readline().strip()
if line == '':
break
print line
f.close()
Yes, that would be a way to do it.
in "textfile", i added some spaces in and then ran the code, it prints
out the lines without the spaces in front. I double checked "textfile"
and it does contains some lines with spaces in front.
Is it true that "readline().strip()" perform the removing of spaces in
front of a line as well?
Am I missing something here? You've just written a program
that does exactly what you ask, and shown to yourself that
*yes*, calling "strip()" does indeed strip off whitespace.
You state "it prints out the lines without the spaces in
front". Yup...I'd guess that's pretty strong evidence that
"readline().strip()" performs the removing of spaces in
front of a line as well.
Is it documented anywhere?


Run a python interpreter shell.
help("".strip)
help("".rstrip)
help("".lstrip)


This within-the-interpreter is one of my favorite language
features in Python (and maddening when 3rd-party library
developers don't document everything as well as the base
modules are)

-tkc


Mar 4 '06 #3
ei***********@yahoo.com writes:
hi
wish to ask a qns on strip
i wish to strip all spaces in front of a line (in text file)

f = open("textfile","rU")
while (1):
line = f.readline().strip()
if line == '':
break
print line
f.close()

in "textfile", i added some spaces in and then ran the code, it prints
out the lines without the spaces in front. I double checked "textfile"
and it does contains some lines with spaces in front.
Is it true that "readline().strip()" perform the removing of spaces in
front of a line as well? Is it documented anywhere?
I am using Windows environment. thanks


jupiter:~ $ pydoc string.strip
Help on function strip in string:

string.strip = strip(s, chars=None)
strip(s [,chars]) -> string

Return a copy of the string s with leading and trailing
whitespace removed.
If chars is given and not None, remove characters in chars instead.
If chars is unicode, S will be converted to unicode before stripping.
--
Jorge Godoy <go***@ieee.org>

"Quidquid latine dictum sit, altum sonatur."
- Qualquer coisa dita em latim soa profundo.
- Anything said in Latin sounds smart.
Mar 4 '06 #4
On Fri, 03 Mar 2006 20:01:30 -0800, eight02645999 wrote:
hi
wish to ask a qns on strip
i wish to strip all spaces in front of a line (in text file)

f = open("textfile","rU")
while (1):
line = f.readline().strip()
if line == '':
break
print line
f.close()

in "textfile", i added some spaces in and then ran the code, it prints
out the lines without the spaces in front. I double checked "textfile"
and it does contains some lines with spaces in front.
Is it true that "readline().strip()" perform the removing of spaces in
front of a line as well? Is it documented anywhere?


As well as what? I don't understand your question. You seem to be asking,
"is it true that strip() strips whitespace just like it is supposed to?".

If so, the answer is yes.

The strip() method doesn't care whether the string it is called from is a
literal like " hello world! ".strip(), or whether it comes from a text
file like f.readline().strip(). All it cares about is that the object is a
string.

You may also find the help() command useful. From an interactive session,
call help(some_object). Remember that functions without the brackets are
objects too: you can say help("".strip).
--
Steven.

Mar 4 '06 #5
ei***********@yahoo.com wrote:
hi
wish to ask a qns on strip
i wish to strip all spaces in front of a line (in text file)

f = open("textfile","rU")
while (1):
line = f.readline().strip()
if line == '':
break
print line
f.close()

in "textfile", i added some spaces in and then ran the code, it prints
out the lines without the spaces in front. I double checked "textfile"
and it does contains some lines with spaces in front.
Is it true that "readline().strip()" perform the removing of spaces in
front of a line as well? Is it documented anywhere?
I am using Windows environment. thanks

If you are using Windows then navigate to Start | All Programs | Python
2.4 | Python Manuals. Click the "Index" tab and enter "strip".

You will see that there is a strip() function in module string, and that
strings have a .strip() method.

This is called "Reading the Documentation". Do it more. Your time is no
more valuable than that of those who help on this list. Please try to
respect it by answering questions as best you can *before* resorting to
the list.

That way you'll continue to be a welcome visitor.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd www.holdenweb.com
Love me, love my blog holdenweb.blogspot.com

Mar 4 '06 #6
ei***********@yahoo.com wrote:
f = open("textfile","rU")
while (1):
********line*=*f.readline().strip()
********if*line*==*'':
****************break
********print*line
f.close()


Be warned that your code doesn't read the whole file if that file contains
lines with only whitespace characters. If you want to print every line,
change the loop to

while 1:
line = f.readline()
if line == "":
break
print line.strip()

i. e. don't strip() the line until you have tested for an empty string. The
idiomatic way to loop over the lines in a file is

for line in f:
print line.strip()

Peter
Mar 4 '06 #7

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

Similar topics

9
by: hokiegal99 | last post by:
This script works as I expect, except for the last section. I want the last section to actually remove all spaces from the front and/or end of filenames. For example, a file that was named " test ...
4
by: Jay Chan | last post by:
I am trying to export data from a SQLServer database into a text file using a stored procedure. I want to be able to read it and debug it easily; therefore, I want all the columns to indent nicely....
1
by: Andy Jefferies | last post by:
I'm having problems stripping out the whitespace at the beginning of a particular element. In the XML snippet I've highlighted tabs and returns as ^I and ^M respectively: <para> ^I ^I ...
1
by: Kevin Auch | last post by:
Hi, I'm working on a little application which analyse source code file from VS.NET. But I'm in front of a big problem. The source generated by VS.NET is automatically "indented", but when I...
4
by: Lu | last post by:
Hi, i am currently working on ASP.Net v1.0 and is encountering the following problem. In javascript, I'm passing in: "somepage.aspx?QSParameter=<RowID>Chèques</RowID>" as part of the query...
135
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
1
by: paul.hester | last post by:
Hi all, Does anyone have a good regular expression to strip tab and new line characters in the Render method? Thanks, Paul
4
by: micronack | last post by:
{NOTE . = space} I made a script to allow me to edit my webpages online through a text area in my cgi script but I've run into a problem. For whatever reason the text area seems to put spaces in...
14
by: ryan k | last post by:
Hello. I have a string like 'LNAME PASTA ZONE'. I want to create a list of those words and basically replace all the whitespace between them with one space so i could just do...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.