1. What is Stringio/cstringio?
This module provides a class in which an instance of this class can be read and written as a file, in fact a string cache, or a memory file, to read and write.
Stringio and file objects have a common parent class iobase, so the method is basically consistent, and Stringio can be seen as a memory file.
Think about it, Stringio may be the equivalent of Java StringBuffer or StringBuilder ... Whether it can also be used as a write file cache, when I want to write a lot of bits and pieces of content when I write the content to Stringio, when the Stringio is finished I read it again write to disk, this will not be able to implement the cache write O (∩_∩) o haha ~
In py2.x, Cstringio is a C-language version of Stringio, it has a slightly better performance, but py3.x has no cstringio.
2. How to use
The use of Stringio is similar to normal file objects, here is a simple example:
#! /usr/bin/pythonfrom io import stringioif __name__ = = ' __main__ ': f = Stringio () f.write (' first\n ') f.write (' second\n ') F_content = F.getvalue () print (f_content)
Output:
Firstsecond
Resources:
1. https://docs.python.org/2/library/stringio.html
Python module Stringio/cstringio (memory file)