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// . 前者是对标准字符串类型的 ...
随机推荐
- Spark Streaming事务处理彻底掌握
本篇文章主要从二个方面展开: 一.Exactly Once 二.输出不重复 事务: 银行转帐为例,A用户转账给B用户,B用户可能收到多笔钱,如何保证事务的一致性,也就是说事务输出,能够输出且只会输出一 ...
- C++和.net的集合类对应
Here's what I've found (ignoring the old non-generic collections): Array - C array, though the .NE ...
- yeoman-angular-gulp
1.安装 yeoman npm install -g yo gulp bower 2.安装genarate-angular //npm install generator-angular npm in ...
- Visual Studio 2008破解90天试用期
Visual Studio 2008破解90天试用期变成正式版的方法: 第一种方法(安装前用): 把iso文件中的setup文件夹中的setup.sdb文件中的[Product Key]下的一行的原来 ...
- 关于csdn博客中案例效果的动态演示
在曾经一篇博文中,网友评论说要是案例效果是动态演示的就好了,我认为说的非常是有道理.由于一个简单的截图不能非常好的展示案例效果.要是有一张gif图能动态的播放案例效果就再好只是了.在这里提供一个小软件 ...
- spring启动方式
spring有三种启动方式,使用ContextLoaderServlet,ContextLoaderListener和ContextLoaderPlugIn.看一下ContextLoaderListe ...
- MVC3循环添加数据
foreach (var item in orderdetails) { var billdetails = new BillDetail(){BillCode = billoflading.Bill ...
- javascript实现掉落弹出层------Day29
顾名思义.所谓"掉落弹出层".就是出现一个弹出层,而出现的位置是从上方向下掉落.掉落到指定的位置停止,这样分析起来.和"右下角弹出提醒对话框"比起来,确有异曲同 ...
- java Socket Tcp示例三则(服务端处理数据、上传文件)
示例一: package cn.itcast.net.p5.tcptest; import java.io.BufferedReader;import java.io.IOException;impo ...
- 虚拟机和宿主机不能互ping的解决办法等
1. 虚拟机和宿主机不能互ping的解决办法:禁用无关虚拟网卡. 2. 有时有效光驱设备为cdrom1. 3. CentOS 6.3 图形界面切换用户:System->Log Out