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// . 前者是对标准字符串类型的 ...
随机推荐
- Windows10下安装pytorch并导入pycharm
1.安装Anaconda 下载:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 安装Anaconda3,最新版本的就可以了,我安装的是5. ...
- C# 使用Newtonsoft.Json序列化自定义类型
Json.Net是一个读写Json效率比较高的.Net框架.Json.Net 使得在.Net环境下使用Json更加简单.通过Linq To JSON可以快速的读写Json,通过JsonSerializ ...
- [Debug] Debug Node.js Application by using Chrome Dev tools
For example you have a server.js file, and you want to debug some problems; What you can do is: node ...
- Cocos2d-x 3.0多线程异步资源载入代码
// AppDelegate.cpp bool AppDelegate::applicationDidFinishLaunching() { - - FlashScene* scene = Flash ...
- 折腾开源WRT的AC无线路由之路-1
Tags: tomato, dd-wrt, Netgear, NightHawk, R7000, RT-AC68U, RT-AC66U, N66U, N56U, WRT1900AC, Archer C ...
- 使用Git下载Hadoop的到本地Eclipse开发环境
使用Git下载Hadoop的到本地Eclipse开发环境 博客分类: Hadoop *n*x MacBook Air hadoopgitmaveneclipsejava 问题场景 按照官网http: ...
- C#.NET常见问题(FAQ)-在VS程序如何取消.vshost的进程
双击执行一个EXE程序,会有两个进程,程序关闭之后,貌似只能关闭你的程序,附加的vshost.exe仍然存在 在调试页面,改成release,同时取消最后一项启用承载进程 在生成页面,将高级选 ...
- Struts2(七)基础小结
一.struts2和action 二.Result 三.struts.xml 四.namespace 第一种绝对路径 <form action="${pageContext.reque ...
- Mysql自己主动备份
Mysql自己主动备份 批处理命令: set"Ymd=%date:~,4%%date:~5,2%%date:~8,2%" set"hMs=%time:~,2%%time: ...
- Linux中-POSIX 线程详解
一种支持内存共享的简捷工具 摘自https://www.ibm.com/developerworks/cn/linux/thread/posix_thread1/ 线程是有趣的 了解如何正确运用线 ...