手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Python模拟sed在每行添加##

[日期:2014-01-05] 来源:Linux社区  作者:world77 [字体:大 中 小]

我们在平常的工作中有时候需要对摸一个文件进行操作,比如在一个文件的每行前面添加##之类的,在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]#

好了,这次的python介绍就到这里,我将为大家陆续模拟一些sed的简单功能,希望大家能喜欢

推荐阅读:

《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python脚本获取Linux系统信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

Python 的详细介绍:请点这里
Python 的下载地址:请点这里

linux
相关资讯       sed  Python模拟sed 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款