[Tutor] Tutor Digest, Vol 38, Issue 1

Jay Mutter III jmutter at uakron.edu
Sun Apr 1 13:40:36 CEST 2007


Alan thanks for the response;


> Message: 8
> Date: Sun, 1 Apr 2007 08:54:02 +0100
> From: "Alan Gauld" <alan.gauld at btinternet.com>
> Subject: Re: [Tutor] Another parsing question
> To: tutor at python.org
> Message-ID: <eunoeu$prr$1 at sea.gmane.org>
> Content-Type: text/plain; format=flowed; charset="iso-8859-1";
> 	reply-type=original
>
>
> "Jay Mutter III" <jmutter at uakron.edu> wrote
>
>> for line in s:
>>     jay = patno.findall(line)
>>     jay2 = "".join(jay[0])
>>     print jay2
>>
>> and it prints fine up until line 111 which is a line that had
>> previously returned [ ] since a number didn't exist on that line and
>> then exits with
>
>> IndexError: list index out of range
>
> Either try/catch the exception or add an
> if not line: continue  # or return a default string
>
>> And as long as i am writing, how can I delete a return at the end of
>> a line if the line ends in a certain pattern?
>>
>> For instance, if line ends with the abbreviation  No.
>
> if line.endswith(string): line = line.rstrip()
>

For some reason this never works for me;
i am using an intel imac with OS X 10.4.9 which has python 2.3.5

inp = open('test.txt','r')
s = inp.readlines()
for line in s:
     if line.endswith('No.'):
         line = line.rstrip()
     print line
and it never ever removes the line feed.  (These are unix \r  
according to Text wrangler)
I am beginning to think that it is a problem with readlines.

But then i thought well why not

inp = open('test.txt','r')
s = inp.readlines()
for line in s:
     if line.endswith('No.'):
         line += s.next()
     print line,

however that doesn't work either which leads me to believe that it is  
me and my interpretation of the above.

Thanks

Jay



>> I want to join the current line with next line.
>> Are lists immutable or can they be changed?
>
> lists can be changed, tuples cannot.
>
> HTH,
>
> -- 
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
>
>
>
>
> ------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> End of Tutor Digest, Vol 38, Issue 1
> ************************************



More information about the Tutor mailing list