0
下面的代码工作正常,直到我添加了正则表达式行。当我发表评论时,代码再次运作...我很难过。我只用一个正则表达式来同时搜索三种不同类型的字符串(ascii,hex,string)。任何帮助表示赞赏,谢谢!正则表达式导致一个错误,我似乎无法找到
elif searchType =='2':
print " Directory to be searched: c:\Python27 "
directory = os.path.join("c:\\","Python27")
userstring = raw_input("Enter a string name to search: ")
userStrHEX = userstring.encode('hex')
userStrASCII = ' '.join(str(ord(char)) for char in userstring)
regex = re.compile("(%s|%s|%s)" % (re.escape(userstring), re.escape(userStrHEX), re.escape(userStrASCII))
for root,dirname, files in os.walk(directory):
for file in files:
if file.endswith(".log") or file.endswith(".txt"):
f=open(os.path.join(root, file))
for line in f.readlines():
#if userstring in line:
if regex.search(line):
print "file: " + os.path.join(root,file)
break
else:
print "String NOT Found!"
break
f.close()
I'我不是Python的人,但是你的缩进在这里是正确的吗?例如,最终的'else'是否与最终的'if'有关? – 2011-04-27 14:02:31
哪一行尤其是你问的? 'regex = re.compile ...'行?如果不存在,是否为'if regex.search(line):'也缺席? – 2011-04-27 14:03:35
你得到的错误是什么? – 2011-04-27 14:04:34