python 刪除特定字元所在行

2021-10-20 05:53:25 字數 1166 閱讀 4020

查詢檔案中含有特殊字串的行

#!/usr/bin/python

# -*- coding:utf-8 -*-

import re

file1 =

open

('test.txt'

,'r+'

)istxt = re.

compile

(r'.*if.*'

,re.i)

for line in file1.readlines():

line = line.strip(

) ifstr = re.findall(istxt,line)

if ifstr:

print ifstr

刪除特定行(建立新檔案,把不含字元的那些行寫進新檔案中,重新命名檔案成原來的檔名稱)

複製**

'''

'''import re

import os

file1 =

open

('test.txt'

,'r+'

)new_file =

'new_test.txt'

ifnot os.path.exists(new_file)

: os.system(r"touch {}"

.format

(new_file)

)file2 =

open

('new_test.txt'

,'r+'

)istxt = re.

compile

(r'.*if.*'

,re.i)

for line in file1.readlines():

line = line.strip(

) ifstr = re.findall(istxt,line)

if ifstr:

line =

else

: file2.write(line +

'\n'

)file1.close(

)file2.close(

)#重新命名檔案

os.rename(

"new_test.txt"

,"test.txt"

)

刪除特定字元

題目大意 編寫乙個高效率的演算法來刪除字串中的給定字元。比如,this is a student 是源字串,aeiou 是刪除字串,則結果為 ths s stdnt 一開始想到新建兩個陣列,乙個用下標表示刪除字串中出現的字元,乙個用來儲存經過處理的字串,結果輸出第二個陣列,如下 include in...

SQL刪除特定字元

請教sql刪除特定字元 sql語句為 update table name set field name replace field name from str to str 說明 table name 表的名字,field name 欄位名,from str 需要替換的字串,to str 替換成的字...

python學習之字串刪除特定字元

語法 str replace old,new max 樣例 str this is string example.wow this is really string print str replace is was print str replace is was 3 print str 執行結果 ...