MRAB wrote:
On May 16, 4:21 pm, Lisa <[EMAIL PROTECTED]> wrote:
I am reading in data from a text file. I want to enter each value on
the line into a list and retain the order of the elements.  The number
of elements and spacing between them varies, but a typical line looks
like:

'   SRCPARAM   1 6.35e-07 15.00  340.00   1.10      3.0   '
Using builtin functions:

   ax = '   SRCPARAM   1 6.35e-07 15.00  340.00   1.10      3.0   '
    >>> ax.replace('  ','')  # replace all double spaces with nothing
   ' SRCPARAM 1 6.35e-07 15.00340.00 1.103.0 '
    >>> ax.replace('  ','').strip() # strip leading/trailing white spaces
   'SRCPARAM 1 6.35e-07 15.00340.00 1.103.0'
    >>> ax.replace('  ','').strip().split(' ') # split string into a
   list, using remaining white space as key
   ['SRCPARAM', '1', '6.35e-07', '15.00340.00', '1.103.0']

   def getElements( str ):
       return str.replace( '  ', '' ).strip().split(' ')


sph

--
HEX: 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to