-1

Can anyone tell me how to set the output of a command to a variable?

Basically, I'm looking for the Python equivalent to this bash example:

blah="ajsdlk akajl <ajksd@ajksldf.com>"
blah=$(echo "$blah" | cut -d '<' -f 2 | cut -d '>' -f 1)
echo "$blah"
ajksd@ajksldf.com
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
BeMy Friend
  • 751
  • 1
  • 6
  • 11
  • You can use subprocess. This looks very close to http://stackoverflow.com/questions/4760215/running-shell-command-from-python-and-capturing-the-output –  Aug 30 '15 at 06:06
  • possible duplicate of [Capture stdout from a script in Python](http://stackoverflow.com/questions/5136611/capture-stdout-from-a-script-in-python) – tripleee Aug 30 '15 at 06:14

2 Answers2

5

You may use string.split

>>> blah="ajsdlk akajl <ajksd@ajksldf.com>"
>>> blah.split('<')[1].split('>')[0]
'ajksd@ajksldf.com'
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
1

If a function returns a string, just capture its return value. If you're looking to capture the standard output from a function, wrap it with a StringIO wrapper.

tripleee
  • 175,061
  • 34
  • 275
  • 318