當前位置:首頁 » 編程語言 » python調用sed

python調用sed

發布時間: 2022-03-13 18:18:20

1. python 實現文本要求,詳見問題補充 不能使用 shell sed awk

importre
withopen("test.txt","r")asA:
Save_Line=A.readlines()
Save_Line[49]=re.sub("haiwao","haiwai",Save_Line[49])
Save_Blank=''
forline,nameinenumerate(filter(lambdax:x.rstrip(),Save_Line)):
ifname=='':Save_Blank=Save_Blank+','+str(line)
iflineinrange(5):printname
ifname.endwith("hai"):printname
printSave_Blank

withopen("test.txt","w")asA:
A.writelines(Save_Blank)

2. 請教python里怎麼調用sed或者awk亞

通過system函數調用shell命令
os.system('sed、awk')

3. python中執行sed命令操作源文件時出現錯誤

你的範例裡面,srcfile是一個python object,放到sedcmd字元串裡面,sed會把它當文件名處理,自然找不到文件。這個問題可以用subprocess解決。


#!/usr/bin/envpython2
#coding=utf-8

"""

"""

importsubprocess


defmain():
withopen('/qye/python/mytestpython/tmp.txt','rb')asf:
o=subprocess.check_output(["sed","-n","s/{//g;p"],stdin=f)
#or:
#o=subprocess.check_output("sed-n's/{//g;p'",shell=True,stdin=f)


if__name__=='__main__':
main()

4. 請教python里怎麼調用sed或者awk亞

os.popen()可以調用系統命令 當然也包括sed 和awk

5. 文件中的某一行sed命令為什麼比Python慢

windows批處理沒有內置sed這樣的文本編輯程序你有3個選擇:安裝windows版本的sed,比如sed for windows或者架構一個linux環境,比如cygwin使用其他腳本或程序等價實現,比如perl, python

6. 在linux系統中 用shell 或者python 運行 grep 命令 但要查找的內容是很多個 並想把結果放到一個變數中

如果多個內容是與關系,可以多個grep用管道相連,是或關系,可用正則式
如grep '\(ab|cd\)' a.txt 是查包含 ab或cd的行, grep 'ab' a.txt | grep 'cd' 是查包含ab和cd的行
要將結果賦到變數 a=`grep '\(ab|cd\)' a.txt ` 就行了
在python里就相當好辦了,用不著grep啦,字元串的find函數就行啦

7. python 或 批處理 替換文件中的內容

這個用sed就可以了:

sed -i 's/version=.*/version=0/' config.ini

如果有多個ini文件:

sed -i 's/version=.*/version=0/' *.ini

另外如果是windows系統,沒有自帶sed命令。可以到這里下載:
http://gnuwin32.sourceforge.net/packages/sed.htm

8. 如何只在Python注釋前加#,而不在整行前加#

sed 's/^[^#]/#&/' file.txt >output.txt

註:
s是sed中的替換命令。
第一個^表示行首位置,[^#]表示非#號,合起來就表示要匹配不以#開頭的行。
後面用&來原封不動引用前面匹配到的行內容,在其前面加上#號。

9. Python,AWK,sed 之間有哪些區別

sed - when you need to do simple text transforms on files.
awk - when you only need simple formatting and summarization or transformation of data.
perl - for almost any task, but especially when the task needs complex regular expressions.
python - for the same tasks that you could use Perl for.

10. shell+sed+awk和perl和python的區別

shell+sed+awk是linux下的語言和開發工具,很多系統類基礎類用起來比較快捷
perl和python是2個非常強大的語言,但是個人認為python更強,適應廣泛

熱點內容
shell腳本需要編譯鏈接 發布:2025-07-02 00:04:20 瀏覽:473
微信如何重設密碼 發布:2025-07-02 00:02:27 瀏覽:545
java代碼基礎 發布:2025-07-02 00:00:46 瀏覽:304
煙花的代碼c語言 發布:2025-07-01 23:56:04 瀏覽:224
安卓默認打開文件方式怎麼修改 發布:2025-07-01 23:30:38 瀏覽:862
壓縮機接線座 發布:2025-07-01 23:17:48 瀏覽:662
iqoo瀏覽器緩存路徑 發布:2025-07-01 23:12:38 瀏覽:691
明日之後如何獲得最新伺服器 發布:2025-07-01 23:12:35 瀏覽:51
tv加密頻道 發布:2025-07-01 23:10:58 瀏覽:625
如何找到5d4通信密碼 發布:2025-07-01 23:03:35 瀏覽:235