from cStringIO import StringIO output = StringIO() output.write("This goes into the buffer.") print >>output, 'And so does this.' #连接字符串 print output.getvalue() output.close() input = StringIO('Inital value for read buffer') print input.read() input.write(' leon-test') #如果加载的是cStringIO模块,则抛错,一旦StringIO()构造函数有值,则write方法无效 input.seek(0) print input.read()