Searching and extracting WH-word from a file line by line with Python and regex -


i have file has 1 sentence per line. trying read file , search if sentence question using regex , extract wh-word sentences , save them file according order appeared in first file.

this have far..

def whwordextractor(inputfile):     try:         openfileobject = open(inputfile, "r")         try:              whpattern = re.compile(r'(.*)who|what|how|where|when|why|which|whom|whose(\.*)', re.ignorecase)             openfileobject infile:                 line in infile:                      whword = whpattern.search(line)                     print whword  # save whword extracted inputfile whword.txt file #                    writefileobject = open('whword.txt','a')                    #                    if not whword: #                        writefileobject.write('none' + '\n') #                    else: #                        whquestion = whword    #                        writefileobject.write(whquestion+ '\n')           finally:             print 'done. wh-word extracted.'             openfileobject.close()     except ioerror:         pass  result after running code above: set([]) 

is there doing wrong here? grateful if can point out me.

not sure if it's you're looking for, try this:

def whwordextractor(inputfile):     try:         whpattern = re.compile(r'who|what|how|where|when|why|which|whom|whose', re.ignorecase)         open(inputfile, "r") infile:             line in infile:                 whmatch = whpattern.search(line)                 if whmatch:                     whword = whmatch.group()                     print whword                     # save file                 else:                     # no match     except ioerror:         pass 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -