Python Learning notes-Stringio and Bytesio

Source: Internet
Author: User
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.

    • The GetValue () method is used to get the content written in the Stringio

    • The close () method closes Stringio and frees up memory.

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.