[Tutor] Convert a string of numbers to a list

Kent Johnson kent37 at tds.net
Thu Feb 26 12:45:05 CET 2009


On Thu, Feb 26, 2009 at 2:00 AM,  <kkwaiser at umich.edu> wrote:
> Hi,
>
> I am trying to convert an output from subprocess.Popen() from a byte string
> to a list of numbers.  This is what Popen returns:
>
> print SAL.stdout.readlines()
>
> ['[335, 180, 201, 241, 199]\r\n']

For one line:
In [11]: s = '[335, 180, 201, 241, 199]\r\n'

In [12]: map(int, s.strip('[]\r\n').split(', '))
Out[12]: [335, 180, 201, 241, 199]

Modify appropriately to handle a list of lines.

Kent


More information about the Tutor mailing list