简体   繁体   中英

search a file using python

my problem is when I search a pdf file using python. I search it line by line so suppose I have a line contains:

"this this this %this"

so if we put x = "this this this %this" and I want to count the number of "this" and ignore what proceeds "%" as it is a comment. the code is :

if re.search("%",x):
    new_line = x.split()
    for g in new_line:
        if re.search("%",g):
            break
        elif g == "this":
            counter = counter+1
    print (counter)

but what if I have the following:

x = "this this this %this %this" the second percentage ends the comment and I want to skip "this" which is between "%" and count the last one

have any one any Idea to do it ?

You could try

x = re.sub("%[^%]*%?", "", x);

Demo: http://regex101.com/r/tE6rL7

data = "this this this %this %this"

data = ' '.join(data.split('%')[::2])

data # => "this this this  this"

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