python - Remove whitespace from a regex search in a file -


i'm attempting remove whitespace selected string search using regexp. code works continues return error i'm not sure how resolve ...?

elif searchtype =='2':       print "  directory searched: c:\python27 "       directory = os.path.join("c:\\","sqa_log")       userstring = raw_input("enter string name search: ")       userstrhex = userstring.encode('hex')       userstrascii = ' '.join(str(ord(char)) char in userstring)       regex = re.compile(r"(%s|%s|%s)" % ( re.escape( userstring ), re.escape( userstrhex ), re.escape( userstrascii )))       choice = raw_input("type 1 search whitespace. type 2 search ignoring whitespace: ")       if choice == '1':            root,dirname, files in os.walk(directory):               file in files:                   if file.endswith(".log") or file.endswith(".txt"):                      f=open(os.path.join(root, file))                      i,line in enumerate(f.readlines()):                          result = regex.search(line)                          if regex.search(line):                             print " "                             print "line: " + str(i)                             print "file: " + os.path.join(root,file)                             print "string type: " + result.group()                             print " "                        f.close()       re.purge()                     if choice == '2':          root,dirname, files in os.walk(directory):              file in files:                  if file.endswith(".log") or file.endswith(".txt"):                     f=open(os.path.join(root, file))                     i,line in enumerate(f.readlines()):                         result = regex.search(re.sub(r'\s', '',line))                         if regex.search(line):                            print " "                            print "line: " + str(i)                            print "file: " + os.path.join(root,file)                            print "string type: " + result.group()                            print " "                      f.close()                             re.purge() 

this error returns:

line: 9160 file: c:\sqa_log\13.00.log string type: rozelle07  line: 41 file: c:\sqa_log\news.txt string type: 526f7a656c6c653037  line: 430 file: c:\sqa_log\readme.txt  traceback (most recent call last):   file "c:\sqa_log\cmd_simple.py", line 226, in <module>     sqast().cmdloop()   file "c:\python27\lib\cmd.py", line 142, in cmdloop     stop = self.onecmd(line)   file "c:\python27\lib\cmd.py", line 219, in onecmd     return func(arg)   file "c:\sqa_log\cmd_simple.py", line 147, in do_search     print "string type: " + result.group() attributeerror: 'nonetype' object has no attribute 'group' 

it appears regex.search fails on line whitespaces stripped, succeeds when whitespaces there. haven't given regex's definition, can't tell why happens, if replace if regex.search(line) if result: shouldn't error.

the reason error re.search returns special value, none, when doesn't find match, instead of match object. none evaluates false inside boolean expressions, can use in if statement, doesn't have attributes, , that's why result.group() fails when result none.

btw: want use re.gsub(r'\s+', '', line) instead of re.sub(r'\s', '', line) if want remove all occurrences of whitespace in line , not first one.

fixed code:

for i,line in enumerate(f.readlines()):     result = regex.search(re.gsub(r'\s+', '', line))     if result:        print ... 

Comments