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
Post a Comment