|
Size: 1450
Comment:
|
Size: 2617
Comment:
|
| Deletions are marked like this. | Additions are marked like this. |
| Line 35: | Line 35: |
| rtext = sys.argv[2] input = sys.stdin output = sys.stdout |
rtext = sys.argv[2] input = sys.stdin #标准输入 output = sys.stdout #标准输出 |
| Line 39: | Line 39: |
| input = open(sys.argv[3]) | input = open(sys.argv[3]) #数据源文件 |
| Line 41: | Line 41: |
| output = open(sys.argv[4], 'w') for s in input.xreadlines( ): output.write(s.replace(stext, rtext)) |
output = open(sys.argv[4], 'w') #数据目的文件 for s in input.xreadlines( ): #读入文件的每一行 output.write(s.replace(stext, rtext)) #处理,并写入目的文件 |
| Line 47: | Line 48: |
| Translator Note : file.xreadlines() is deprecated since Python 2.3, use the following instead for line in file: process(line) |
|
| Line 50: | Line 54: |
| 处理方法很简单,也是方法漂亮的地方。杀鸡不用牛刀。上面食谱是可以执行的脚本 This recipe is really simple, but that's what beautiful about it梬hy do complicated stuff when simple stuff suffices? The recipe is a simple main script, as indicated by the leading "shebang" line. The script looks at its arguments to determine the search text, the replacement text, the input file (defaulting to standard input), and the output file (defaulting to standard output). Then, it loops over each line of the input file, writing to the output file a copy of the line with the substitution performed on it. That's all! For accuracy, it closes both files at the end. |
文章来自《Python cookbook》. 翻译仅仅是为了个人学习,其它商业版权纠纷与此无关!
-- 61.182.251.99 [DateTime(2004-09-20T19:38:11Z)] TableOfContents
描述
文件中查找替换字符串
问题
You need to change one string into another throughout a file. 替换文件中特定字符串
解决
String substitution is most simply performed by the replace method of string objects. The work here is to support reading from the specified file (or standard input) and writing to the specified file (or standard output):
使用string的replace方法可以简单实现字符串替换操作。从指定文件(或标准输入)读入内容,进行处理,然后写入指定文件(或标准输出),工作就完成了。
import os, sys
nargs = len(sys.argv)
if not 3 <= nargs <= 5:
print "usage: %s search_text replace_text [infile [outfile]]" % \
os.path.basename(sys.argv[0])
else:
stext = sys.argv[1]
rtext = sys.argv[2]
input = sys.stdin #标准输入
output = sys.stdout #标准输出
if nargs > 3:
input = open(sys.argv[3]) #数据源文件
if nargs > 4:
output = open(sys.argv[4], 'w') #数据目的文件
for s in input.xreadlines( ): #读入文件的每一行
output.write(s.replace(stext, rtext)) #处理,并写入目的文件
output.close( )
input.close( )
Translator Note : file.xreadlines() is deprecated since Python 2.3, use the following instead
for line in file:
process(line)
讨论 Discussion
处理方法很简单,也是方法漂亮的地方。杀鸡不用牛刀。上面食谱是可以执行的脚本 This recipe is really simple, but that's what beautiful about it梬hy do complicated stuff when simple stuff suffices? The recipe is a simple main script, as indicated by the leading "shebang" line. The script looks at its arguments to determine the search text, the replacement text, the input file (defaulting to standard input), and the output file (defaulting to standard output). Then, it loops over each line of the input file, writing to the output file a copy of the line with the substitution performed on it. That's all! For accuracy, it closes both files at the end.
...
