python标准库介绍——15 fileinput 模块详解
``fileinput`` 模块允许你循环一个或多个文本文件的内容, 如 [Example 2-1 #eg-2-1] 所示. ====Example 2-1. 使用 fileinput 模块循环一个文本文件====[eg-2-1] ```
File: fileinput-example-1.py import fileinput
import sys for line in fileinput.input("samples/sample.txt"):
sys.stdout.write("-> ")
sys.stdout.write(line) *B*-> We will perhaps eventually be writing only small
-> modules which are identified by name as they are
-> used to build larger ones, so that devices like
-> indentation, rather than delimiters, might become
-> feasible for expressing local structure in the
-> source language.
-> -- Donald E. Knuth, December 1974*b*
``` 你也可以使用 ``fileinput`` 模块获得当前行的元信息 (meta information). 其中包括 ``isfirstline`` ,
``filename`` , ``lineno`` , 如 [Example 2-2 #eg-2-2] 所示. ====Example 2-2. 使用 fileinput 模块处理多个文本文件====[eg-2-2] ```
File: fileinput-example-2.py import fileinput
import glob
import string, sys for line in fileinput.input(glob.glob("samples/*.txt")):
if fileinput.isfirstline(): # first in a file?
sys.stderr.write("-- reading %s --\n" % fileinput.filename())
sys.stdout.write(str(fileinput.lineno()) + " " + string.upper(line)) *B*-- reading samples\sample.txt --
1 WE WILL PERHAPS EVENTUALLY BE WRITING ONLY SMALL
2 MODULES WHICH ARE IDENTIFIED BY NAME AS THEY ARE
3 USED TO BUILD LARGER ONES, SO THAT DEVICES LIKE
4 INDENTATION, RATHER THAN DELIMITERS, MIGHT BECOME
5 FEASIBLE FOR EXPRESSING LOCAL STRUCTURE IN THE
6 SOURCE LANGUAGE.
7 -- DONALD E. KNUTH, DECEMBER 1974*b*
``` 文本文件的替换操作很简单. 只需要把 ``inplace`` 关键字参数设置为 1 ,
传递给 ``input`` 函数, 该模块会帮你做好一切. [Example 2-3 #eg-2-3] 展示了这些. ====Example 2-3. 使用 fileinput 模块将 CRLF 改为 LF====[eg-2-3] ```
File: fileinput-example-3.py import fileinput, sys for line in fileinput.input(inplace=1):
# convert Windows/DOS text files to Unix files
if line[-2:] == "\r\n":
line = line[:-2] + "\n"
sys.stdout.write(line)
```
python标准库介绍——15 fileinput 模块详解的更多相关文章
- python标准库介绍——12 time 模块详解
==time 模块== ``time`` 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装. 给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 ...
- python标准库介绍——27 random 模块详解
==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...
- python标准库介绍——10 sys 模块详解
==sys 模块== ``sys`` 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. === 处理命令行参数=== 在解释器启动后, ``argv`` 列表包含了传递给脚本的所有 ...
- python标准库介绍——30 code 模块详解
==code 模块== ``code`` 模块提供了一些用于模拟标准交互解释器行为的函数. ``compile_command`` 与内建 ``compile`` 函数行为相似, 但它会通过测试来保证 ...
- python标准库介绍——19 mmap 模块详解
==mmap 模块== (2.0 新增) ``mmap`` 模块提供了操作系统内存映射函数的接口, 如 [Example 2-13 #eg-2-13] 所示. 映射区域的行为和字符串对象类似, 但数据 ...
- python标准库介绍——8 operator 模块详解
==operator 模块== ``operator`` 模块为 Python 提供了一个 "功能性" 的标准操作符接口. 当使用 ``map`` 以及 ``filter`` 一类 ...
- python标准库介绍——5 re模块详解
== re 模块== "Some people, when confronted with a problem, think 'I know, I'll use regular expres ...
- python标准库介绍——36 popen2 模块详解
==popen2 模块== ``popen2`` 模块允许你执行外部命令, 并通过流来分别访问它的 ``stdin`` 和 ``stdout`` ( 可能还有 ``stderr`` ). 在 pyth ...
- python标准库介绍——23 UserString 模块详解
==UserString 模块== (2.0 新增) ``UserString`` 模块包含两个类, //UserString// 和 //MutableString// . 前者是对标准字符串类型的 ...
随机推荐
- MongoDB学习笔记(一)--基础
Insert MongoD ...
- Java Object Clone
Java Object Clone User user = new User(); user.setName("tom"); User user1 = new User(); us ...
- 转: 用 Eclipse 平台进行 C/C++ 开发
http://www.ibm.com/developerworks/cn/linux/opensource/os-ecc/index.html
- Cognos 报表在列表上面显示汇总
一直以来,Cognos Report Studio设计报表的时候,汇总默认显示在列表下方: 1如图,拖一个列表 2运行如下,数据显示正常按日期排序 3选中订单笔数.订单金额,添加自动汇总 4:运行,可 ...
- kettle根据参数动态派生列
抽取数据的时候没有日期字段,需要根据抽取日期自动生成月份,如下图结构 表输入_参数部分,接收来自其他系统传过来的参数(JAVA程序或者页面),具体设置如图 在查询数据时候派生列 运行模型的时候,给参数 ...
- Transformer中引用iqd作为数据源的时提示TR1008无法连接问题
刚接触tr的同行们可能都会遇到这么一个问题,当然我也遇到了. 今天把问题的解决办法详细的说明一下 从问题的表面来看是从cognos.ini文件缺少了什么配置东西,但是cognos10的安装中没有cog ...
- 解决Windows Git Bash中文乱码问题
在git 安装目录 etc 下面 添加以下配置信息 1,/etc/gitconfig: [gui] encoding = utf-8 #代码库统一用urf-8,在git gui中可以正常显示中文 [i ...
- Android实现Material Design风格的设置页面(滑动开关控件)
前言 本文链接 http://blog.csdn.net/never_cxb/article/details/50763271 转载请注明出处 參考了这篇文章 Material Design 风格的设 ...
- ArcGIS Pro体验01——申请、下载、安装
ArcGIS Pro采用了Ribbon界面风格,看起来好漂亮,听起来很强大,就是不知道用起来怎么样,在网上看到一个ArcGIS Pro Beta2版本,下载下来,安装启动,好眼熟,像Office201 ...
- cocos2d-x3.0创建第一个jsb游戏
第一步: 最新的cocos2d-x.下载地址https://github.com/cocos2d/cocos2d-x github上最新的引擎,值得注意的是官网上发布的引擎是稳定版.选择哪种就看个人喜 ...