[Tutor] Another parsing question

Jay Mutter III jmutter at uakron.edu
Sun Apr 1 02:45:21 CEST 2007


Kent;
Again thanks for the help.
i am not sure if this is what you menat but i put

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

Traceback (most recent call last):
   File "patentno2.py", line 12, in ?
     jay2 = "".join(jay[0])
IndexError: list index out of range


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.
I want to join the current line with next line.
Are lists immutable or can they be changed?

Thanks again

jay

On Mar 31, 2007, at 2:27 PM, Kent Johnson wrote:

> Jay Mutter III wrote:
>> I have the following that I am using to extract "numbers' from a file
>> ...
>> which yields the following
>> [('1', '337', '912')]
> > ...
>> So what do i have above ? A list of tuples?
>
> Yes, each line is a list containing one tuple containing three  
> string values.
>
>> How do I  send the output to a file?
>
> When you print, the values are automatically converted to strings  
> by calling str() on them. When you use p2.write(), this conversion  
> is not automatic, you have to do it yourself via
>   p2.write(str(jay))
>
> You can also tell the print statement to output to a file like this:
>   print >>p2, jay
>
>> Is there a way to get the output as
>> 1337912  instead of   [('1', '337', '912')]  ?
>
> In [4]: jay=[('1', '337', '912')]
>
> jay[0] is the tuple alone:
> In [6]: jay[0]
> Out[6]: ('1', '337', '912')
>
> Join the elements together using an empty string as the separator:
> In [5]: ''.join(jay[0])
> Out[5]: '1337912'
> In [7]:
>
> Kent



More information about the Tutor mailing list