python模拟sed在每行添加##
生活随笔
收集整理的這篇文章主要介紹了
python模拟sed在每行添加##
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
? ? ?我們?cè)谄匠5墓ぷ髦杏袝r(shí)候需要對(duì)摸一個(gè)文件進(jìn)行操作,比如在一個(gè)文件的每行前面添加##之類的,在shell中這個(gè)需求很簡(jiǎn)單,用sed單行就能搞定,下面我們來看看一個(gè)文件:
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的單行命令來搞定這個(gè)需求很簡(jiǎn)單,看下代碼:
[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]#
看看,果然夠強(qiáng)大的sed啊,下面我來給大家介紹介紹如何用python實(shí)現(xiàn)這個(gè)有時(shí)候經(jīng)常需要的操作,直接上代碼了:
[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')
代碼實(shí)在很簡(jiǎ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
呵呵,效果出來了吧,但是稍有缺陷,這個(gè)需要操作的對(duì)象文件我們是寫死在代碼里面的,如何把文件名作為參數(shù)傳遞給腳本呢,我們需要修改,以實(shí)現(xiàn)如下幾個(gè)功能:
1. 需要把操作的文件作為參數(shù)傳給腳本
2.需要對(duì)操作的對(duì)象進(jìn)行判斷,是否存在
3.如果腳本運(yùn)行錯(cuò)誤,需要有友好的提示效果
基于以上的需求,給出代碼的最終版本,代碼如下:
[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介紹就到這里,我將為大家陸續(xù)模擬一些sed的簡(jiǎn)單功能,希望大家能喜歡
轉(zhuǎn)載于:https://blog.51cto.com/world77/1348328
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的python模拟sed在每行添加##的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 让乔布斯立足肩上的C语言之父
- 下一篇: python很简单。。。。