删除模式之间的文本 [英] deleting texts between patterns

查看:111
本文介绍了删除模式之间的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



说我有一个文本文件


line1

line2

line3

line4

line5

line6

abc

line8< --- to被删除

line9< ---将被删除

line10< ---将被删除

line11< ---待删除

line12< ---将被删除

line13< ---将被删除

xyz

line15

line16

line17

line18


我想删除那行介于''abc''和''xyz'之间,打印

其余部分。这是最好的方法吗?我应该将所有内容放入列表中,获取abc和xyz的索引,然后弹出

元素吗?或者任何其他更好的方法?

谢谢

hi
say i have a text file

line1
line2
line3
line4
line5
line6
abc
line8 <---to be delete
line9 <---to be delete
line10 <---to be delete
line11 <---to be delete
line12 <---to be delete
line13 <---to be delete
xyz
line15
line16
line17
line18

I wish to delete lines that are in between ''abc'' and ''xyz'' and print
the rest of the lines. Which is the best way to do it? Should i get
everything into a list, get the index of abc and xyz, then pop the
elements out? or any other better methods?
thanks

推荐答案



mickle。 .. @ hotmail.com写道:

mickle...@hotmail.com wrote:

说我有一个文本文件

line1
line2
line3
line4
line5
line6
abc
第8行< ---要删除
第9行< ---要删除
第10行< - - 要删除
第11行< ---要删除
第12行< ---要删除
第13行< ---要删除
xyz <第17行
第16行
第17行

我想删除''abc''和''xyz''之间的行并打印<其余的线条。这是最好的方法吗?我应该把所有内容都放到列表中,获取abc和xyz的索引,然后弹出
元素吗?或者任何其他更好的方法?
谢谢
hi
say i have a text file

line1
line2
line3
line4
line5
line6
abc
line8 <---to be delete
line9 <---to be delete
line10 <---to be delete
line11 <---to be delete
line12 <---to be delete
line13 <---to be delete
xyz
line15
line16
line17
line18

I wish to delete lines that are in between ''abc'' and ''xyz'' and print
the rest of the lines. Which is the best way to do it? Should i get
everything into a list, get the index of abc and xyz, then pop the
elements out? or any other better methods?
thanks




换句话说......

lines = open(''test.txt '')。readlines()

for line in lines [lines.index(''abc \'')+ 1:lines.index(''xyz \ n'')] :

lines.remove(行)

换行:

打印行,

正则表达式在这种情况下更好

import re

pat = re.compile(''abc \ n。*?xyz \ n'',re。 DOTALL)

print re.sub(pat,'''',open(''test.txt'')。read())



In other words ...
lines = open(''test.txt'').readlines()
for line in lines[lines.index(''abc\n'') + 1:lines.index(''xyz\n'')]:
lines.remove(line)
for line in lines:
print line,

Regular expressions are better in this case
import re
pat = re.compile(''abc\n.*?xyz\n'', re.DOTALL)
print re.sub(pat, '''', open(''test.txt'').read())

< br>

写道:

说我有一个文本文件

line1
line2
line3
line4
line5
line6
abc
第8行< ---要删除
第9行< ---要删除
第10行< ---要删除
第11行< ---要删除
第12行< ---要删除
第13行< ---要删除
xyz
line15
line16
line17
line18

我希望删除''abc''和''xyz''之间的行并打印
其余的线。这是最好的方法吗?我应该把所有内容都放到列表中,获取abc和xyz的索引,然后弹出
元素吗?或者任何其他更好的方法?
谢谢
hi
say i have a text file

line1
line2
line3
line4
line5
line6
abc
line8 <---to be delete
line9 <---to be delete
line10 <---to be delete
line11 <---to be delete
line12 <---to be delete
line13 <---to be delete
xyz
line15
line16
line17
line18

I wish to delete lines that are in between ''abc'' and ''xyz'' and print
the rest of the lines. Which is the best way to do it? Should i get
everything into a list, get the index of abc and xyz, then pop the
elements out? or any other better methods?
thanks




这样的东西(未经测试的代码):


def过滤( f,停止,重启):

f = iter(f)

for line in f:

yield line

如果行==停止:

中断

for f行:

如果行==重启:

产量线

休息

for line in f:

yield line


for line in过滤(打开(''thefile'')," abc \ n"," xyz \ n"):

打印行



Something like this (untested code):

def filtered(f, stop, restart):
f = iter(f)
for line in f:
yield line
if line==stop:
break
for line in f:
if line==restart:
yield line
break
for line in f:
yield line

for line in filtered(open(''thefile''), "abc\n", "xyz\n"):
print line




< mi ******* @ hotmail.com> skrev i meddelandet新闻:11 ********************** @ i40g2000cwc.googlegr oups.com ...

<mi*******@hotmail.com> skrev i meddelandet news:11**********************@i40g2000cwc.googlegr oups.com...
hi
说我有一个文字文件

line1
line2
line3
line4
line5
line6
abc
line8< ---要删除
第9行< ---要删除
第10行< ---要删除
第11行< ---要删除
line12< ---要删除
line13< ---要删除
xyz
line15
line16
line17
line18

我希望删除abc和xyz之间的行,并打印其余的行。这是最好的方法吗?我应该把所有内容都放到列表中,获取abc和xyz的索引,然后弹出
元素吗?或者任何其他更好的方法?
hi
say i have a text file

line1
line2
line3
line4
line5
line6
abc
line8 <---to be delete
line9 <---to be delete
line10 <---to be delete
line11 <---to be delete
line12 <---to be delete
line13 <---to be delete
xyz
line15
line16
line17
line18

I wish to delete lines that are in between ''abc'' and ''xyz'' and print
the rest of the lines. Which is the best way to do it? Should i get
everything into a list, get the index of abc and xyz, then pop the
elements out? or any other better methods?




简单的问题是什么?

emit = True

for open in line(" q.txt"):

if line ==" xyz\\\
":

emit = True
>
如果发出:

打印行,

如果行==" abc \ n":

emit = False


循环? (如果您不想在输出中包含

的模式,这也很容易调整。


打印到文件而不是stdout,只需用f.write调用替换打印行。


< / F>



what''s wrong with a simple

emit = True
for line in open("q.txt"):
if line == "xyz\n":
emit = True
if emit:
print line,
if line == "abc\n":
emit = False

loop ? (this is also easy to tweak for cases where you don''t want to include
the patterns in the output).

to print to a file instead of stdout, just replace the print line with a f.write call.

</F>


这篇关于删除模式之间的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆