[Tutor] Convert a string of numbers to a list

A.T.Hofkamp a.t.hofkamp at tue.nl
Thu Feb 26 12:40:04 CET 2009


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']
> 
> Except the string will be much longer in practice.
> 
> I've experimented with .rstrip and .split and I've looked over the csv 
> module but have not been able to figure anything out.   My only 
> restriction is to avoid writing to a file.

Don't know about the csv module, but if you want to do it in plain Python:

readlines() returns a list of lines, so do the following for each line in the 
list:

1. chop off the "[" and "]\r\n"
2. split on ","
3. convert to integer values by means of int()

> I have a feeling I'm missing something incredibly simple.  Any help 
> would be greatly appreciated,

Please post what you have, and what you are stuck on, then we can guide you 
through the problem.



Sincerely,
Albert


More information about the Tutor mailing list