0

I'm using a complicated python module called CAMFR. In one function, it calculates a value that I want to use (to plot or otherwise), but unfortunately the module prints this value to the python console but doesn't return it as a variable!

(I have poked through the source code to see if I can recompile the module to return the values, but this looks excessively difficult at my programming level, considering it's written in C++ and uses Boost etc. I just don't get it unfortunately.)

SO, option number two is to grab the text printed to the console and parse out the value I need.

How can I intercept or otherwise acquire this function's console output text in Python (2.7)? (I will RegEx it afterwards.)

Thanks!

Here is an example of the text printed to the Python console:

<<a lot of other output, and then at the end of the function:>>
...
@ 1.05554 4.65843e-05 5.54592 0.0903205 1
@ 1.05554 2.87907e-05 3.42757 0.0903205 1
@ 1.05554 2.87907e-05 3.42757 0.0903205 1
Done pass 1: lambda 1.05554, gain 3.42757

I ultimately want to grab the Lambda=1.05544 & Gain=3.42757 values, for example, and shove them into variables. Grabbing the entire console output of this one function would allow me to do that via a subsequent RegEx search, so I'm looking for a way to do that.

I apologize if there is another thread with the answer I need, I could not figure out google search terms that got me what I needed. Thanks for your patience & generous help!

Demis
  • 5,278
  • 4
  • 23
  • 34
  • possible duplicate of [Can I redirect the stdout in python into some sort of string buffer?](http://stackoverflow.com/questions/1218933/can-i-redirect-the-stdout-in-python-into-some-sort-of-string-buffer) – a p Oct 07 '14 at 19:23
  • Looks like http://stackoverflow.com/questions/1218933/can-i-redirect-the-stdout-in-python-into-some-sort-of-string-buffer might help you out. Or maybe http://stackoverflow.com/questions/14197009/how-can-i-redirect-print-output-of-a-function-in-python – a p Oct 07 '14 at 19:23
  • 1
    Ah, it did not occur to me to search for 'stdout' (i kept searching for 'console'). Thanks, these answer my question perfectly. – Demis Oct 07 '14 at 19:30

0 Answers0