Object for storing strings in Python
Posted
by evg
on Stack Overflow
See other posts from Stack Overflow
or by evg
Published on 2010-03-12T14:03:31Z
Indexed on
2010/03/12
14:37 UTC
Read the original article
Hit count: 155
class MyWriter:
def __init__(self, stdout):
self.stdout = stdout
self.dumps = []
def write(self, text):
self.stdout.write(smart_unicode(text).encode('cp1251'))
self.dumps.append(text)
def close(self):
self.stdout.close()
writer = MyWriter(sys.stdout)
save = sys.stdout
sys.stdout = writer
I use self.dumps
list to store data obtained from prints. Is there a more convenient object for storing string lines in memory? Ideally I want dump it to one big string. I can get it like this "\n".join(self.dumps)
from code above. May be it's better to just concatenate strings - self.dumps += text
?
© Stack Overflow or respective owner