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().st rip()
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().str ip()" perform the removing of spaces in
front of a line as well? Is it documented anywhere?
I am using Windows environment. thanks 6 2685 ei***********@y ahoo.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().st rip() 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().str ip()" 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.readl ine().split())
If you want to normalize space, do this:
line = " ".join(f.readli ne().split())
This should fulfill 90% of your space-transforming requirements.
James
> 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().st rip() 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().str ip()" 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().str ip()" 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 ei***********@y ahoo.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().st rip() 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().str ip()" 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.
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().st rip() 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().str ip()" 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().st rip(). 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_objec t). Remember that functions without the brackets are
objects too: you can say help("".strip).
--
Steven. ei***********@y ahoo.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().st rip() 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().str ip()" 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.blogs pot.com ei***********@y ahoo.com wrote: f = open("textfile" ,"rU") while (1): ********line*=* f.readline().st rip() ********if*line *==*'': *************** *break ********print*l ine 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 " would be
renamed "test" (the 2 spaces before and after the filename removed). Any
suggestions on how to do this?
import os, re, string
print " "
print "--- Remove '%2f' From Filenames ---"
print " "
|
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. This means
I need to append trailing spaces to a text string (such as "Test1 ")
or append leading space in front of a text string that contains a
number (such as " 12.00"). Now, the stored procedure works fine when
I run it in Query Analyzer....
|
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 ^I^M
Some text with occasional highlighting. Some text with occasional^M
highlighting. Some text with occasional <high>highlighting</high>.^M
Some text with occasional highlighting. Some <high>text</high> with^M
occasional highlighting.</para>^M
|
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 analyse these lines
I can't remove the tabs or the spaces at the beginning of the line because
it's not spaces or tabs.
I tried to change the encoding of my StreamReader but it's always the
same...
|
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
string. However, in the code behind when I tried to get the query string
value by calling Request.QueryString("QSParameter"), the value I got is:
"<RowID>Chques</RowID>". The special character "è" has been stripped out.
The web.config file is...
| |
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
which is better. It has become what's known as “religious war” —
a heated fight over trivia. In this essay, i like to explain what is
the situation behind it, and which is proper.
|
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
|
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 front of each line {except for the first} so,
line one
line two
line three
becomes
line one
.line two
.line three
|
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 lala.split(). Thank you!
Ryan Kaskel
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |