如何在python中删除特定行上方的所有行 [英] How to remove all lines above a certain line in python

查看:133
本文介绍了如何在python中删除特定行上方的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个html文件,我想在其中删除以字符串<!DOCTYPE html

I have an html file where I want to remove all lines above the line starting with string <!DOCTYPE html

示例:

HTTP/1.1 400 Bad Request
Content-Type: text/html; charset=utf-8
Date: Sat, 22 Mar 2015 07:56:52 
Connection: close
Content-Length: 2959

<!DOCTYPE html...... extra lines ...

因此,当我搜索字符串<!DOCTYPE的出现时,我需要删除所有行,包括该特定行上方的空白行.在linux中,我们在grep中有一个选项,可以搜索上下两行,然后将其删除.我们可以在Python中做类似的事情吗?

So when I search for the occurrence of string <!DOCTYPE I need to remove all lines including blank ones above this particular line. In linux we have an option in grep which can search for the lines above and below and then delete it. Can we do a similar thing in Python?

推荐答案

stop = "<!DOCTYPE html"

with open('input.html') as infile, open('output.html', 'w') as outfile:
    buff = []
    for line in infile:
        if not line.strip():
            buff.append(line)
            continue
        if line.strip() == stop: break
        outfile.write(''.join(buff))
        buff = []
        outfile.write(line)

这篇关于如何在python中删除特定行上方的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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