[Tutor] Another parsing question

Kent Johnson kent37 at tds.net
Sat Mar 31 20:27:21 CEST 2007


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