• 注册
当前位置:1313e > python >正文

python标准库介绍——18 StringIO 模块详解

==StringIO 模块==[Example 2-8 #eg-2-8] 展示了 ``StringIO`` 模块的使用. 它实现了一个工作在内存的文件对象
(内存文件). 在大多需要标准文件对象的地方都可以使用它来替换.====Example 2-8. 使用 StringIO 模块从内存文件读入内容====[eg-2-8]```
File: stringio-example-1.pyimport StringIOMESSAGE = "That man is depriving a village somewhere of a computer scientist."file = StringIO.StringIO(MESSAGE)print file.read()*B*That man is depriving a village somewhere of a computer scientist.*b*
`````StringIO`` 类实现了内建文件对象的所有方法, 此外还有 ``getvalue`` 方法用来返回它内部的字符串值. 
[Example 2-9 #eg-2-9] 展示了这个方法.====Example 2-9. 使用 StringIO 模块向内存文件写入内容====[eg-2-9]```
File: stringio-example-2.pyimport StringIOfile = StringIO.StringIO()
file.write("This man is no ordinary man. ")
file.write("This is Mr. F. G. Superman.")print file.getvalue()*B*This man is no ordinary man. This is Mr. F. G. Superman.*b*
`````StringIO`` 可以用于重新定向 Python 解释器的输出, 如 [Example 2-10 #eg-2-10] 所示.====Example 2-10. 使用 StringIO 模块捕获输出====[eg-2-10]```
File: stringio-example-3.pyimport StringIO
import string, sysstdout = sys.stdoutsys.stdout = file = StringIO.StringIO()print """
According to Gbaya folktales, trickery and guile
are the best ways to defeat the python, king of
snakes, which was hatched from a dragon at the
world's start. -- National Geographic, May 1997
"""sys.stdout = stdoutprint string.upper(file.getvalue())*B*ACCORDING TO GBAYA FOLKTALES, TRICKERY AND GUILE
ARE THE BEST WAYS TO DEFEAT THE PYTHON, KING OF
SNAKES, WHICH WAS HATCHED FROM A DRAGON AT THE
WORLD'S START. -- NATIONAL GEOGRAPHIC, MAY 1997*b*
```

 

本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 162202241@qq.com 举报,一经查实,本站将立刻删除。

最新评论

欢迎您发表评论:

请登录之后再进行评论

登录