未找到打印搜索的问题

Posted

技术标签:

【中文标题】未找到打印搜索的问题【英文标题】:Problems printing searches not found 【发布时间】:2011-06-21 12:06:29 【问题描述】:

在下面的代码中,程序从用户那里获取字符串数据并将其转换为 ascii 和 hex,并在某个目录中搜索所有 .log 和 .txt 文件以查找纯字符串、十六进制和 ascii 值的字符串。程序打印 # 行、找到的字符串类型以及找到字符串的文件路径。但是,如果找到字符串,我不仅希望它打印文件,我还希望它打印在搜索但未找到的文件中搜索的文件和路径和字符串。我是新手,所以请不要对问题的简单性感到沮丧。我还在学习。谢谢。代码如下:

 elif searchType =='2':
      print "\nDirectory to be searched: " + directory
      print "\nFile result2.log will be created in: c:\Temp_log_files."
      paths = "c:\\Temp_log_files\\result2.log"
      temp = file(paths, "w")
      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(r"(%s|%s|%s)" % ( re.escape( userstring ), re.escape( userStrHEX ), re.escape( userStrASCII )))
      goby = raw_input("Press Enter to begin search (search ignores whitespace)!\n")


      def walk_dir(directory, extensions=""):
          for path, dirs, files in os.walk(directory):
             for name in files:
                if name.endswith(extensions):
                   yield os.path.join(path, name)

      whitespace = re.compile(r'\s+')
      for line in fileinput.input(walk_dir(directory, (".log", ".txt"))):
          result = regex.search(whitespace.sub('', line))
          if result:
              template = "\nLine: 0\nFile: 1\nString Type: 2\n\n"
              output = template.format(fileinput.filelineno(), fileinput.filename(), result.group())

              print output
              temp.write(output)
              break
          elif not result:
              template = "\nLine: 0\nString not found in File: 1\nString Type: 2\n\n"
              output = template.format(fileinput.filelineno(), fileinput.filename(), result.group())

              print output
              temp.write(output)

      else:          
          print "There are no files in the directory!!!"

【问题讨论】:

您确实意识到这将导致它打印找到的每个文件的每一行,对吧?如果那是你想要的,那有什么问题?看代码好像没问题 我同意卡佩特的观点。它说你想打印文件,但你真的想要文件名吗?还有一些示例输出将有助于堆。 【参考方案1】:

伙计们,我认为 user706808 想要搜索文件中所有出现的 searchstring 并且:

如果在文件中找到字符串,则每次出现,然后在每行的基础上,打印行号,文件路径名 如果在文件中找不到字符串,则在每个文件的基础上打印文件路径名(但不是内容)和搜索字符串。 最简单的方法是保持布尔(或整数)跟踪事件(nMatches),然后在关闭文件或路径名脱离上下文之前在末尾打印 no-match-message(如果 nMatches 为 0 或 False) .

你能确认一下吗?假设这就是你想要的, 您需要更改的只是拆分这段代码...

for line in fileinput.input(walk_dir(directory, (".log", ".txt"))):

进入...

for curPathname in walk_dir(directory, (".log", ".txt")):
    nOccurrences = 0
    for line in fileinput.input(curPathname):
        result = regex.search(whitespace.sub('', line))
        if result:
            ...
            nOccurrences += 1  # ignores multiple matches on same line 
        # You don't need an 'elif not result' line, since that should happen on a per-file basis
    # Only get here when we reach EOF
    if (nOccurrences == 0):
        NOW HERE print the "not found" message, for curPathname
    # else you could print "found %d occurrences of %s in ..."

听起来不错?

顺便说一下,您现在可以简单地将 fileinput.filename() 称为“curPathname”。

(您也可能希望将功能抽象为一个函数 find_occurrences(searchstring,pathname),它返回 int 或 Boolean 'nOccurrences'。)

【讨论】:

这不完全是我的目标,但它给了我其他想法并帮助我修改了我的代码......谢谢!

以上是关于未找到打印搜索的问题的主要内容,如果未能解决你的问题,请参考以下文章

如何将 iPhone 与外部设备连接?

lodop安装后无法打印

ps-efgrep未找到命令

未找到 Google 云打印 OAuth 范围

如何在文本文件中搜索单词,如果找到则打印出整行

UIPrintInteractionController - 未找到支持文档格式的属性或未找到支持的格式