All we said before was to read and write real files. In fact, we can also virtual a file in memory to read and write. The official module provided to us by Python is Io.stringio and Io.bytesio.
Io. Stringio
String IO is used to read and write strings. Stringio can pass in a character initialization. For example
String = Stringio ("This is Demo")
For example:
From io Import Stringios = Stringio () s.write ("Yes\nyes") S.seek (0) # Dial the pointer back to the start position, otherwise you will not be reading anything content = S.read () print Content
Stringio creates a File-like object that has all the methods of the file object. Stringio also has two special methods, namely the GetValue () method and the close () method.
Io. Bytesio
Stringio can only handle data of a string type, Bytesio may be used to process binary types of data. The use of Bytesio is similar to that of Stringio
Stringio.stringio
When searching for documents, it was found that there was also a stringio under Stringio, and they were very similar. All Google a bit. In StackOverflow there is an answer:
An in-memory stream for Unicode text. It inherits Textiowrapper.
This module implements a File-like class, Stringio, that reads and writes a string buffer (also known as memory files). Io. Stringio is a class. It handles Unicode. It reflects the preferred Python 3 library structure.
Stringio.stringio is a class. It handles strings. It reflects the Legacy Python 2 library structure.
What should is preferred? Always move forward toward the new library organization. The Io.open should is used to replace the built-in Unicode-unaware open.
Forward. Move forward.
The main idea is that Stringio is the legacy of Python2, followed by IO. Stringio replacement. It is recommended to use Io.stringio.