python模拟sed在每行添加##

简介:

 我们在平常的工作中有时候需要对摸一个文件进行操作,比如在一个文件的每行前面添加##之类的,在shell中这个需求很简单,用sed单行就能搞定,下面我们来看看一个文件:

[root@host-192-168-209-128 py-sed]# cat a.txt
this is a text
this is use for python
this is also user for sed
this is a end test file
[root@host-192-168-209-128 py-sed]#
用sed的单行命令来搞定这个需求很简单,看下代码:
[root@host-192-168-209-128 py-sed]# sed 's/^/##/g' a.txt
##this is a text
##this is use for python
##this is also user for sed
##this is a end test file
[root@host-192-168-209-128 py-sed]#

看看,果然够强大的sed啊,下面我来给大家介绍介绍如何用python实现这个有时候经常需要的操作,直接上代码了:
[root@host-192-168-209-128 py-sed]# cat a.py
#!/usr/bin/env python

with open('a.txt') as f:
       con=f.readlines()
       for i in range(0,len(con)):
               print "###"+con[i].rstrip('\n')
代码实在很简单,看看效果如何吧:
[root@host-192-168-209-128 py-sed]# python a.py
###this is a text
###this is use for python
###this is also user for sed
###this is a end test file

呵呵,效果出来了吧,但是稍有缺陷,这个需要操作的对象文件我们是写死在代码里面的,如何把文件名作为参数传递给脚本呢,我们需要修改,以实现如下几个功能:
1. 需要把操作的文件作为参数传给脚本
2.需要对操作的对象进行判断,是否存在
3.如果脚本运行错误,需要有友好的提示效果
基于以上的需求,给出代码的最终版本,代码如下:

[root@host-192-168-209-128 py-sed]# cat tou.py
#!/usr/bin/env python
'''
edit by qhz
Email : world77@163.coom
This scrip to add "###" at every line for file

'''
def usage():
       print   '''
===============================================
This script to add "###" at every line for file
Use Example:
python script.py file
===============================================
'''
import sys
import os
if len(sys.argv) == 2:
        if os.path.isfile(sys.argv[1]):
                with open(sys.argv[1]) as f:
                       con=f.readlines()
                       for i in range(0,len(con)):
                              print '###'+con[i].strip('\n')
      else:
                print "==============================================="
                 print "Your input file name is not exit or not correct"
                 print "Please try again ,bye ..."
                 print "==============================================="
else:
          usage()
          exit()
[root@host-192-168-209-128 py-sed]#

下面来看看各种情况和效果:
[root@host-192-168-209-128 py-sed]# python tou.py

===============================================
This script to add "###" at every line for file
Use Example:
python script.py file
===============================================

[root@host-192-168-209-128 py-sed]# python tou.py a.tx
===============================================
Your input file name is not exit or not correct
Please try again ,bye ...
===============================================
[root@host-192-168-209-128 py-sed]# python tou.py a.txt
###this is a text
###this is use for python
###this is also user for sed
###this is a end test file
[root@host-192-168-209-128 py-sed]#
本文转自你是路人甲还是霍元甲博客51CTO博客,原文链接http://blog.51cto.com/world77/1348328如需转载请自行联系原作者

world77
相关文章
|
Python
Python基础(输出五行五角星,数量每行递增/输出九九乘法表)
需求:在控制台连续输出五行*, 每一行星星的数量依次递增 思路:使用while循环输出五行内容, 依次输出数字1到5, 再使用数字乘以字符串'*', 即可在每行输出一个星星, 两个星星, ... 五个星星, 从而实现递增
758 1
Python基础(输出五行五角星,数量每行递增/输出九九乘法表)
|
Python
Python:利用蒙特卡洛方法模拟验证概率分布
这个题目可以使用数学方法,将其答案显式地写出来,但是验证解出来的答案是否正确,就可以使用蒙特卡洛方法了。
406 0
Python:利用蒙特卡洛方法模拟验证概率分布
|
机器学习/深度学习 测试技术 Python
蓝桥杯python第二期模拟赛 python 题解
蓝桥杯python第二期模拟赛 python 题解
蓝桥杯python第二期模拟赛 python 题解
python--模拟掷骰子游戏
通过python模拟掷骰子的游戏
python--模拟掷骰子游戏
|
算法 安全 PHP
【高级软件实习】蒙特卡洛模拟 | PRNG 伪随机数发生器 | LCG 线性同余算法 | 马特赛特旋转算法 | Python Random 模块
本篇博客将介绍经典的伪随机数生成算法,我们将 重点讲解 LCG(线性同余发生器) 算法与马特赛特旋转算法,在此基础上顺带介绍 Python 的 random 模块。 本篇博客还带有练习,无聊到喷水的练习,咳咳…… 学完前面的内容你就会了解到 Python 的 Random 模块的随机数生成的实现,是基于马特赛特旋转算法的,比如 random_uniform 函数。而本篇博客提供的练习会让你实现一个基于 LCG 算法的random_uniform,个人认为还是比较有意思的
549 0
【高级软件实习】蒙特卡洛模拟 | PRNG 伪随机数发生器 | LCG 线性同余算法 | 马特赛特旋转算法 | Python Random 模块
|
算法 测试技术 Python
第十四届蓝桥杯第一期模拟赛 python
第十四届蓝桥杯第一期模拟赛 python
第十四届蓝桥杯第一期模拟赛 python
|
Go Python
CSP 202006-2 稀疏矩阵 python 模拟
CSP 202006-2 稀疏矩阵 python 模拟
CSP 202006-2 稀疏矩阵 python 模拟
|
Python
【Python 百练成钢】灯光模拟
【Python 百练成钢】灯光模拟
116 0
【Python 百练成钢】灯光模拟
python实操案例__02—利用prettytable库模拟高铁售票系统
PrettyTable 是python中的一个第三方库,可用来生成美观的ASCII格式的表格,本实操案例用此库完成。
|
安全 数据安全/隐私保护 Python
Python初级案例教学【第二课】(Python 黑客对讲机,模拟个人用户登录,银行金额大写汉字转换)
Python模拟个人用户登录 业务需求: 要求:账号:admin 密码:123 1.登录时给3次机会。 2. 如果成功,显示欢迎xxx。 3. 如果登录失败,显示录入错误你还有x次机会。如果3次机会使用完毕,则显示登录超限,请明天再登录。 Python银行金额大写汉字转换 业务需求: 银行电子支票业务在金额部分需要使用大写的汉字,因此需要将用户录入的数字信息转变为汉字。 • 目前只需完成1~5位整数转换即可。
316 1
Python初级案例教学【第二课】(Python 黑客对讲机,模拟个人用户登录,银行金额大写汉字转换)