Python module Stringio/cstringio (memory file)

Source: Internet
Author: User

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)

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.