[Tutor] Re: string formatting problem

A.M. Kuchling amk@amk.ca
Sun Aug 3 12:12:02 EDT 2003


On Sun, Aug 03, 2003 at 11:06:05AM -0400, Ron A wrote:
>            y = data.readline() 

.readline() leaves a newline on the end, so y is set to a string such as 
"1,2,3,4\n".

>            z = string.split(y, ',') 

This splits only on comma characters, so z is set to ['1', '2', '3', '4\n'].
If you print z[3], that string will include a newline.  

Simple fix: strip off the newline (and any other trailing whitespace) 
with .rstrip():

        y = data.readline().rstrip()

--amk                                                             (www.amk.ca)
If you're using anything besides US-ASCII, I *stringly* suggest Python 2.0.
      -- Uche Ogbuji (A fortuitous typo?), 29 Jan 2001




More information about the Tutor mailing list