Examples of Stringio use of Python modules

Source: Internet
Author: User
Stringio is often used as a string cache, should have a benefit for Stringio, some of his interfaces and file operations are consistent, that is, with the same code, can be used as a file operation or Stringio operation. Like what:
Copy the Code code as follows:


Import string, OS, sys
Import Stringio

def writedata (FD, MSG):
Fd.write (msg)

f = open (' Aaa.txt ', ' W ')

WriteData (F, "xxxxxxxxxxxx")
F.close ()

s = Stringio.stringio ()
WriteData (S, "xxxxxxxxxxxxxx")


Because the file object and Stringio most of the methods are the same, such as read, ReadLine, ReadLines, write, writelines are all there, so that Stringio can be very convenient as a "Memory file object."
Copy the Code code as follows:


Import string
Import Stringio

s = Stringio.stringio ()
S.write ("AAAA")
lines = [' xxxxx ', ' bbbbbbb ']
S.writelines (lines)

S.seek (0)
Print S.read ()

Print S.getvalue ()
S.write ("Ttttttttt")
S.seek (0)
Print S.readlines ()
Print S.len


Stringio also has a corresponding C language version of the implementation, it has better performance, but slightly a little bit of the difference, Cstringio no len and POS attributes.
  • 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.