stripping spaces in front of line

E

eight02645999

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
 
J

James Stroud

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
 
T

Tim Chase

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.

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
 
J

Jorge Godoy

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 <[email protected]>

"Quidquid latine dictum sit, altum sonatur."
- Qualquer coisa dita em latim soa profundo.
- Anything said in Latin sounds smart.
 
S

Steven D'Aprano

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).
 
S

Steve Holden

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
 
P

Peter Otten

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
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,156
Latest member
KetoBurnSupplement
Top