简体   繁体   English

删除所有内容,直到python中的某些字符串或字符

[英]Delete everything until certain string or character in python

I need to read a file and delete everything until a certain string occurs. 我需要读取一个文件并删除所有内容,直到出现某个字符串为止。 It's jut simple and I wrote the code but it do not work. 这很简单,我编写了代码,但是没有用。 It returns me the whole content of the file. 它返回我文件的全部内容。

My input file: 我的输入文件:

/****************************************************************
*
*  Function Name:     ab
*  Input Parameter:   cd
*  output Parameter:  ef
*  Return Value:      hi
*
****************************************************************/

#include "file_a.h"
#include "file_b.h"

static inline function(int a, int b){
...
...
...
...
}

I have to delete everything until : 我必须删除所有内容,直到:

static inline function(int a, int b){

so that this statement will be the first line in the new file. 这样该语句将成为新文件的第一行。

My Code 我的密码

TAG =  'static'

def delete_until(fileName,outfile):
    tag_found = False
    with open ('output.txt',"w") as out:
        with open(fileName,'r') as f:

            for line in f:
                if not tag_found:
                    if line.strip() == TAG:
                        tag_found = True
                    else:
                        out.write(line)

if __name__ == '__main__':
    fileName =  'myfile.txt'
    outfile = 'output.txt'
    delete_until(fileName,outfile)

The new file has again the whole content. 新文件再次具有全部内容。 What I'm doing wrong? 我做错了什么?

If you're analysing code files, they're usually small enough to load into memory. 如果要分析代码文件,它们通常足够小以装入内存。 At that point, a single string.find call should do it. 届时,只需执行一个string.find调用即可。

with open(fileName,'r') as fin, open('output.txt',"w") as fout:
    text = fin.read()
    i = text.find('static')
    if i > -1:
        fout.write(text[i:])

This writes: 写道:

static inline function(int a, int b){
...
...
...
...
}

to output.txt . output.txt


If there's a chance that static appears inside a comment before an actual static function, and assuming the code files you're analysing were written by someone sane, you can check for a newline prepended to the keyword. 如果有一个机会, static评论中出现的实际之前static功能,并假设代码文件你分析由理智的人写,你可以检查一个换行符前面加上关键字。 The only changes are here: 唯一的变化是在这里:

i = text.find('\nstatic') 
if i > -1:
    fout.write(text[i + 1:])

Credit to JFF . 归功于JFF

使用sed

$ sed '1,/static inline function(int a, int b){/d' < your-file.c

Your code fails because of this test: 由于此测试,您的代码失败:

if line.strip() == TAG:

You defined TAG 's content this way: 您是通过以下方式定义TAG的内容的:

TAG =  'static'

but the line you just read is: 但是您刚刚读到的行是:

'static inline function(int a, int b){'

This means you can't compare using just the == operator. 这意味着您不能仅使用==运算符进行比较。 Depending of what are your requirements, either change the value of TAG to match what you are searching (which would be the wisest thing to do, since having a static in the comments would match too), or change the way you look for that string. 根据您的要求,更改TAG的值以匹配要搜索的内容(这是最明智的选择,因为注释中也包含static也可以匹配),或者更改查找该字符串的方式。

def delete_until(fileName,outfile):
    tag_found = False
    with open ('output.txt',"w") as out:
        with open(fileName,'r') as f:

            for line in f:
                if not tag_found:
                    if TAG in line:
                        tag_found = True
                if tag_found:
                    out.write(line)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM