简体   繁体   中英

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.

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 .


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. The only changes are here:

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

Credit to 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 =  '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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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