Connecting Tech Pros Worldwide Forums | Help | Site Map

stripping spaces in front of line

eight02645999@yahoo.com
Guest
 
Posts: n/a
#1: Mar 4 '06
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


James Stroud
Guest
 
Posts: n/a
#2: Mar 4 '06

re: stripping spaces in front of line


eight02645999@yahoo.com wrote:[color=blue]
> 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
>[/color]

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
Tim Chase
Guest
 
Posts: n/a
#3: Mar 4 '06

re: stripping spaces in front of line


> wish to ask a qns on strip[color=blue]
> 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()[/color]

Yes, that would be a way to do it.
[color=blue]
> 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?[/color]

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.
[color=blue]
> Is it documented anywhere?[/color]

Run a python interpreter shell.
[color=blue][color=green][color=darkred]
>>> help("".strip)
>>> help("".rstrip)
>>> help("".lstrip)[/color][/color][/color]

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






Jorge Godoy
Guest
 
Posts: n/a
#4: Mar 4 '06

re: stripping spaces in front of line


eight02645999@yahoo.com writes:
[color=blue]
> 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[/color]

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 <godoy@ieee.org>

"Quidquid latine dictum sit, altum sonatur."
- Qualquer coisa dita em latim soa profundo.
- Anything said in Latin sounds smart.
Steven D'Aprano
Guest
 
Posts: n/a
#5: Mar 4 '06

re: stripping spaces in front of line


On Fri, 03 Mar 2006 20:01:30 -0800, eight02645999 wrote:
[color=blue]
> 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?[/color]

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.

Steve Holden
Guest
 
Posts: n/a
#6: Mar 4 '06

re: stripping spaces in front of line


eight02645999@yahoo.com wrote:[color=blue]
> 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
>[/color]
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

Peter Otten
Guest
 
Posts: n/a
#7: Mar 4 '06

re: stripping spaces in front of line


eight02645999@yahoo.com wrote:
[color=blue]
> f = open("textfile","rU")
> while (1):
> ********line*=*f.readline().strip()
> ********if*line*==*'':
> ****************break
> ********print*line
> f.close()[/color]

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


Closed Thread


Similar Python bytes