日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python模拟sed在每行添加##

發布時間:2025/1/21 python 85 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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]#


? ? 好了,這次的python介紹就到這里,我將為大家陸續模擬一些sed的簡單功能,希望大家能喜歡


轉載于:https://blog.51cto.com/world77/1348328

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的python模拟sed在每行添加##的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。