==StringIO 模块==

[Example 2-8 #eg-2-8] 展示了 ``StringIO`` 模块的使用. 它实现了一个工作在内存的文件对象
(内存文件). 在大多需要标准文件对象的地方都可以使用它来替换. ====Example 2-8. 使用 StringIO 模块从内存文件读入内容====[eg-2-8] ```
File: stringio-example-1.py import StringIO MESSAGE = "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.py import StringIO file = 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.py import StringIO
import string, sys stdout = sys.stdout sys.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 = stdout print 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*
```

python标准库介绍——18 StringIO 模块详解的更多相关文章

  1. python标准库介绍——27 random 模块详解

    ==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...

  2. python标准库介绍——12 time 模块详解

    ==time 模块== ``time`` 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装. 给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 ...

  3. python标准库介绍——20 cStringIO 模块详解

    ==cStringIO 模块== ``cStringIO`` 是一个可选的模块, 是 ``StringIO`` 的更快速实现. 它的工作方式和 ``StringIO`` 基本相同, 但是它不可以被继承 ...

  4. python标准库介绍——10 sys 模块详解

    ==sys 模块== ``sys`` 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. === 处理命令行参数=== 在解释器启动后, ``argv`` 列表包含了传递给脚本的所有 ...

  5. python标准库介绍——24 traceback 模块详解

    ==traceback 模块== [Example 2-18 #eg-2-18] 展示了 ``traceback`` 模块允许你在程序里打印异常的跟踪返回 (Traceback)信息, 类似未捕获异常 ...

  6. python标准库介绍——30 code 模块详解

    ==code 模块== ``code`` 模块提供了一些用于模拟标准交互解释器行为的函数. ``compile_command`` 与内建 ``compile`` 函数行为相似, 但它会通过测试来保证 ...

  7. python标准库介绍——8 operator 模块详解

    ==operator 模块== ``operator`` 模块为 Python 提供了一个 "功能性" 的标准操作符接口. 当使用 ``map`` 以及 ``filter`` 一类 ...

  8. python标准库介绍——36 popen2 模块详解

    ==popen2 模块== ``popen2`` 模块允许你执行外部命令, 并通过流来分别访问它的 ``stdin`` 和 ``stdout`` ( 可能还有 ``stderr`` ). 在 pyth ...

  9. python标准库介绍——33 thread 模块详解

    ?==thread 模块== (可选) ``thread`` 模块提为线程提供了一个低级 (low_level) 的接口, 如 [Example 3-6 #eg-3-6] 所示. 只有你在编译解释器时 ...

随机推荐

  1. idea 本地调用zookeeper配置

  2. 【图解】javaScript组成结构

  3. java核心技术36讲

    https://time.geekbang.org/column/intro/82?utm_source=website&utm_medium=infoq&utm_campaign=8 ...

  4. (字符串)最长公共子序列(Longest-Common-Subsequence,LCS)

    问题: 最长公共子序列就是寻找两个给定序列的子序列,该子序列在两个序列中以相同的顺序出现,但是不必要是连续的. 例如序列X=ABCBDAB,Y=BDCABA.序列BCA是X和Y的一个公共子序列,但是不 ...

  5. php平滑升级

    php平滑升级 1.既然是平滑升级,是不需要宕机,也不需要停止服务,是不存在关闭网站的说法 2.而升级最后的重启,重启的是PHP加载的配置文件,不影响你当前运行的网站 3.php-fpm是属于Fast ...

  6. java 将字符串数组变为字典顺序排序后的字符串数组

    import java.util.ArrayList; import java.util.Collections; import java.util.List; public class String ...

  7. win7官方下载地址

    win7官方下载地址 http://microsoftstore.me/category/microsoft/windows/

  8. dos下遍历目录和文件的代码(主要利用for命令)(转)

    ===== 文件夹结构 ============================================= D:\test ---A Folder 1 |-----A file 1.txt | ...

  9. 【安卓】给gallery内"控件"挂载事件,滑动后抬起手指时也触发事件(滑动时不应触发)的解决、!

    思路: 1.gallery内控件挂载事件(如:onClickListener)的方法类似listview,可直接在baseAdapter.getView内给控件挂载(详细方法百度). 2.貌似没问题, ...

  10. 【svn】Centos搭建svn服务器环境

    1.需求描述 在Centos系统中搭建svn服务器环境 2.搭建过程 2.1 yum安装svn [root@localhost /]# yum install svn  2.2 新建目录存储svn目录 ...