python類比sed在每行添加##

來源:互聯網
上載者:User

     我們在平常的工作中有時候需要對摸一個檔案進行操作,比如在一個檔案的每行前面添加##之類的,在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的簡易功能,希望大家能喜歡


本文出自 “你是路人甲還是霍元甲” 部落格,請務必保留此出處http://world77.blog.51cto.com/414605/1348328

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.