MrM*_*rMr 13 python regex python-3.x
我试图删除我的正则表达式匹配的所有行(正则表达式只是寻找任何包含yahoo的行).每个匹配都在它自己的行上,因此不需要多行选项.
这就是我到目前为止......
import re
inputfile = open('C:\\temp\\Scripts\\remove.txt','w',encoding="utf8")
inputfile.write(re.sub("\[(.*?)yahoo(.*?)\n","",inputfile))
inputfile.close()
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
回溯(最近调用最后一次):第170行,在子返回_compile(模式,标志).sub(repl,string,count)TypeError:期望的字符串或缓冲区
Ash*_*ary 14
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,
Run Code Online (Sandbox Code Playgroud)
这是@Ashwini Chaudhary的答案的Python 3变体,删除包含pattern来自给定的正则表达式的所有行filename:
#!/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()
Run Code Online (Sandbox Code Playgroud)
它假定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()
Run Code Online (Sandbox Code Playgroud)
您必须阅读文件尝试类似:
import re
inputfile = open('C:\\temp\\Scripts\\remove.txt','w',encoding="utf8")
inputfile.write(re.sub("\[(.*?)yahoo(.*?)\n","",inputfile.read()))
file.close()
outputfile.close()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14954 次 |
| 最近记录: |