[Tutor] file I/O

ak@silmarill.org ak@silmarill.org
Wed, 11 Jul 2001 16:47:33 -0400


On Thu, Jul 12, 2001 at 08:35:39AM +1200, Massey, Craig wrote:
> Is there a "standard" way of removing the newline, like chomp in Perl?

there's strip() method:

>>> s = 'aljsk\n'
>>> s
'aljsk\n'
>>> print s
aljsk

>>> s.strip()
'aljsk'

>>> help(s.strip)
Help on built-in function strip:

strip(...)
    S.strip() -> string
        
        Return a copy of the string S with leading and trailing
        whitespace removed.

> 
> -----Original Message-----
> From: Michael P. Reilly
> [mailto:arcege@dsl092-074-184.bos1.dsl.speakeasy.net]
> Sent: Thursday, July 12, 2001 8:31 AM
> To: kp87@lycos.com
> Cc: tutor@python.org
> Subject: Re: [Tutor] file I/O
> 
> 
> kevin parks wrote
> > 
> > Hi. I am a little confused as to why this hangs when i run it. I am trying
> to
> 
> Off hand, I would say it is the end condition.  The readline method
> returns an empty string (false) when the end of file is reached, not a
> string with a space.
> 
> > 	done = 0
> > 	while not done:
> > 		aLine = infile.readline()
> > 		if aLine != " ":
>                 if aLine != "":  # not at EOF, so write out
> 
> > 			f.write(aLine + '\n')
> > 		else:
> > 			done = 1
> 
> Also, readline returns a whole line of text, including the newline.
> You probably do not need to append an additional newline.
> 
>   -Arcege
> 
> -- 
> +----------------------------------+-----------------------------------+
> | Michael P. Reilly                | arcege@speakeasy.net              |
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> DISCLAIMER:  This electronic message together with any attachments is 
> confidential.  If you are not the intended recipient, do not copy, disclose or 
> use the contents in any way.  Please also advise us by return e-mail that you 
> have received the message and then please destroy.  Carter Holt Harvey is not 
> responsible for any changes made to this message and / or any attachments after 
> sending by Carter Holt Harvey.  We use virus scanning software but exclude all 
> liability for viruses or anything similar in this email or any attachment.
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: http://silmarill.org/cymbaline