title: Splitting out the output of ps using Python
body: <p>On Linux, the command ps aux outputs a list of processes with multiple columns for each stat. e.g.</p>
  
  <pre><code>USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
  ...
  postfix  22611  0.0  0.2  54136  2544 ?        S    15:26   0:00 pickup -l -t fifo -u
  apache   22920  0.0  1.5 198340 16588 ?        S    09:58   0:05 /usr/sbin/httpd
  </code></pre>
  
  <p>I want to be able to read this in using Python and split out each row and then each column so they can be used as values.</p>
  
  <p>For the most part, this is not a problem:</p>
  
  <pre><code>ps = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE).communicate()[0]
  processes = ps.split('\n')
  </code></pre>
  
  <p>I can now loop through processes to get each row and split it out by spaces, for example</p>
  
  <pre><code>sep = re.compile('[\s]+')
  for row in processes:
      print sep.split(row)
  </code></pre>
  
  <p>However, the problem is that the last column, the command, sometimes has spaces in. In the example above this can be seen in command </p>
  
  <pre><code>pickup -l -t fifo -u
  </code></pre>
  
  <p>which would be split out as</p>
  
  <pre><code>['postfix', '22611', '0.0', '0.2', '54136', '2544', '?', 'S', '15:26', '0:00', 'pickup', '-l', '-t', 'fifo', '-u']
  </code></pre>
  
  <p>but I really want it as:</p>
  
  <pre><code>['postfix', '22611', '0.0', '0.2', '54136', '2544', '?', 'S', '15:26', '0:00', 'pickup -l -t fifo -u']
  </code></pre>
  
  <p>So my question is, how can I split out the columns but when it comes to the command column, keep the whole string as one list element rather than split out by spaces?</p>