簡體   English   中英

使用Python刪除匹配正則表達式的所有行

[英]Using Python to Remove All Lines Matching Regex

我試圖刪除我的正則表達式匹配的所有行(正則表達式只是尋找任何包含yahoo的行)。 每個匹配都在它自己的行上,因此不需要多行選項。

這就是我到目前為止......

import re
inputfile = open('C:\\temp\\Scripts\\remove.txt','w',encoding="utf8")

inputfile.write(re.sub("\[(.*?)yahoo(.*?)\n","",inputfile))

inputfile.close()

我收到以下錯誤:

回溯(最近調用最后一次):第170行,在子返回_compile(模式,標志).sub(repl,string,count)TypeError:期望的字符串或緩沖區

如果要修改原始文件,請使用fileinput模塊:

import re
import fileinput
for line in fileinput.input(r'C:\temp\Scripts\remove.txt', inplace = True):
   if not re.search(r'\byahoo\b',line):
      print line,

這是@Ashwini Chaudhary的答案的Python 3變體,從給定filename刪除包含正則表達式pattern所有行:

#!/usr/bin/env python3
"""Usage: remove-pattern <pattern> <file>"""
import fileinput
import re
import sys

def main():
    pattern, filename = sys.argv[1:] # get pattern, filename from command-line
    matched = re.compile(pattern).search
    with fileinput.FileInput(filename, inplace=1, backup='.bak') as file:
        for line in file:
            if not matched(line): # save lines that do not match
                print(line, end='') # this goes to filename due to inplace=1

main()

它假定locale.getpreferredencoding(False) == input_file_encoding否則它可能會破壞非ascii字符。

無論當前語言環境是什么,或者對於具有不同編碼的輸入文件,使其工作:

#!/usr/bin/env python3
import os
import re
import sys
from tempfile import NamedTemporaryFile

def main():
    encoding = 'utf-8'
    pattern, filename = sys.argv[1:]
    matched = re.compile(pattern).search
    with open(filename, encoding=encoding) as input_file:
        with NamedTemporaryFile(mode='w', encoding=encoding,
                                dir=os.path.dirname(filename),
                                delete=False) as outfile:
            for line in input_file:
                if not matched(line):
                    print(line, end='', file=outfile)
    os.replace(outfile.name, input_file.name)

main()

您必須閱讀文件嘗試類似於:

import re
inputfile = open('C:\\temp\\Scripts\\remove.txt','w',encoding="utf8")

inputfile.write(re.sub("\[(.*?)yahoo(.*?)\n","",inputfile.read()))

file.close()
outputfile.close()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM