0

I'm storing the result of a external .py file in a variable called output. The standard stdout is, for example: ['229', '229', '229', '230', '230', '230']. To do so, after some research, I implemented this class:

try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

class Capturing(list):
    def __enter__(self):
        self._stdout = sys.stdout
        sys.stdout = self._stringio = StringIO()
        return self
    def __exit__(self, *args):
        self.extend(self._stringio.getvalue().splitlines())
        del self._stringio    # free up some memory
        sys.stdout = self._stdout

When I check the output content with print(output) it seems fine. The stdout of my other .py file was stored.

My problem: I can't do almost anything with the output content.

Ex.:

1- When I try to replace characters: AttributeError: 'Capturing' object has no attribute 'replace',

2- When I try to do the same above, but using "translate": AttributeError: 'Capturing' object has no attribute 'translate'

3- Check if it has numbers:

if any(char in '0123456789' for char in output):
    print('True')
else:
    print('False')

This ^ works fine if I manually assign values to output.

I expect some explanation why this is happening. I'm feeling that is something really basic. I'm just started with python.

0 Answers0