python标准库介绍——18 StringIO 模块详解
==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 模块详解的更多相关文章
- python标准库介绍——27 random 模块详解
==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...
- python标准库介绍——12 time 模块详解
==time 模块== ``time`` 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装. 给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 ...
- python标准库介绍——20 cStringIO 模块详解
==cStringIO 模块== ``cStringIO`` 是一个可选的模块, 是 ``StringIO`` 的更快速实现. 它的工作方式和 ``StringIO`` 基本相同, 但是它不可以被继承 ...
- python标准库介绍——10 sys 模块详解
==sys 模块== ``sys`` 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. === 处理命令行参数=== 在解释器启动后, ``argv`` 列表包含了传递给脚本的所有 ...
- python标准库介绍——24 traceback 模块详解
==traceback 模块== [Example 2-18 #eg-2-18] 展示了 ``traceback`` 模块允许你在程序里打印异常的跟踪返回 (Traceback)信息, 类似未捕获异常 ...
- python标准库介绍——30 code 模块详解
==code 模块== ``code`` 模块提供了一些用于模拟标准交互解释器行为的函数. ``compile_command`` 与内建 ``compile`` 函数行为相似, 但它会通过测试来保证 ...
- python标准库介绍——8 operator 模块详解
==operator 模块== ``operator`` 模块为 Python 提供了一个 "功能性" 的标准操作符接口. 当使用 ``map`` 以及 ``filter`` 一类 ...
- python标准库介绍——36 popen2 模块详解
==popen2 模块== ``popen2`` 模块允许你执行外部命令, 并通过流来分别访问它的 ``stdin`` 和 ``stdout`` ( 可能还有 ``stderr`` ). 在 pyth ...
- python标准库介绍——33 thread 模块详解
?==thread 模块== (可选) ``thread`` 模块提为线程提供了一个低级 (low_level) 的接口, 如 [Example 3-6 #eg-3-6] 所示. 只有你在编译解释器时 ...
随机推荐
- iframe之onload事件小记
项目上做了一个具有wizard(向导)功能的菜单导航页面,子页面的引入通过主页面上iframe的src属性切换实现.为了有个良好的交互体验,每次更新iframe的src时,主页面上都显示一个模态的lo ...
- Declaration Merging with TypeScript
原文:https://blog.oio.de/2014/03/21/declaration-merging-typescript/ Why might you need this? There can ...
- idea启动崩溃问题
idea启动崩溃问题 内存已经给到1024m了: 注意到项目比较大,有个参数ReservedCodeCasheSize,把这个修改为1024m, 学习了:https://www.cnblogs.com ...
- jsAnim简单使用教程
引入jsAnim.js 定义动画元素 元素需要有position:relative;或者position:absolute;属性 添加js <!DOCTYPE HTML> <html ...
- Bootstrap3实现的响应式幻灯滑动效果个人作品集/博客网站模板
在线演示 本地下载 如果你想学习如何开发和使用的话,请参考我们免费的课程: Bootstrap3开发滑动风格的博客网站模板
- 用L脚本语言实现"L脚本语言控制台"
下载Windows平台解释引擎 L脚本语言中能够将随意字符串当做一行L脚本语言程序来运行.通过循环接收用户输入,就是一个控制台IDE了 #scp #scp没有控制台IDE?我们自己用scp来实现一个 ...
- LoadRunner Controller: 压力机
前提条件 1. 压力机所在的机器上装了LR agent ,并启用了. 运行下图所示程序,即可启动.启动之后状态栏会出现卫星小图标 2. Controller所在机器的RPC服务开启. 打开运行 –&g ...
- 该内存不能为written
#include <stdio.h>int main(){ char *p = "abc"; printf("%s\n", p); *p = 'd' ...
- geoip+php演示样例:通过ip,获取国家名称和代码
GeoIP + PHP的使用 方法一: 下载 GeoIP 的 PHP 文件geoip.inc,保存为 geoip.inc.php http://sjolzy.cn/php/GeoIP/bak/geoi ...
- Gulp构建前端自动化工作流之:常用插件介绍及使用
在对Gulp有了一个初步的了解之后,我们开始构建一个较为完整的Gulp开发环境. 本文主要分为6个段落: 1. 构建项目目录结构(Directory Structure Build) 2. 插件介绍及 ...