简体   繁体   English

Python:仅打印结果

[英]Python: Print Result Only

import os
def find_method(name):
        i = 0
        found_dic = { "$_GET":[], "$_POST":[], "include":[], "require":[], "mysql_query":[], "SELECT":[], "system":[], "exec":[], "passthru":[], "readfile":[], "fopen":[], "eval":[] }

        for x in file(name, "r"):
                i += 1
                for key in found_dic:
                        if x.strip().find(key) != -1:
                                found_dic[key].append("LINE:"+str(i)+":" + x.strip())

        print "="*20, name, "="*20
        for key in found_dic:
                if found_dic[key]:
                        print " ", "-"*10, key, "-"*10
                        for r in found_dic[key]:
                               print "  ",r

def search(dirname):
        flist = os.listdir(dirname)
        for f in flist:
                next = os.path.join(dirname, f)
                if os.path.isdir(next):
                        search(next)
                else:
                        doFileWork(next)

def doFileWork(filename):
        ext = os.path.splitext(filename)[-1]
        #if ext == '.html': print filename
        if ext == '.php':
               # print "target:" + filename
                find_method(filename)
  1. how can I print only results. 我如何只打印结果。 its prints all name of file eventhough file doesn't have any result in it. 即使文件中没有任何结果,它也会打印文件的所有名称。 I want to make print file name if its has any result in it 我想要打印文件名,如果其中有任何结果

  2. this is about searching word, but it shows every word include like (seaching for include) then it also finds word in sentence and prints all sentence I want to find only word "include" not included in sentence. 这是关于搜索单词的,但它会显示每个单词include like(搜索包括),然后它还会在句子中找到单词并打印所有我只想查找不包含在句子中的单词“ include”的句子。 it's really hard to explain.. I hope to understand.. srry 真的很难解释..我希望了解..对不起

I hope that's what you asked for: 我希望这是您要求的:

for i, line in enumerate(file(name, "r")):
    found = False
    for key in found_dic:
        if key in line.strip():
            found_dic[key].append("LINE:"+str(i)+":" + key)
            found = True

    if found:
        print "="*20, name, "="*20
        for key in found_dic:
            if found_dic[key]:
                print " ", "-"*10, key, "-"*10
                for r in found_dic[key]:
                    print "  ",r

You have to check if you found something if you only want to print the name when you actually found something. 如果您只想在实际找到东西时打印名称,则必须检查是否找到了东西。 Also, you only concatenate key in line 5, because key is what you search. 另外,您仅在第5行中串联键,因为键是您要搜索的键。 And you only want to add what you search. 而且您只想添加搜索内容。

Further changes: I used the enumerate function in line i, its far easier and more readable than incrementing you own i. 进一步的变化:我在第i行中使用了枚举函数,它比增加您拥有的i更容易且更易读。

I also changed the condition in line 10. Using the in keyword here is the more simple and readable way... 我还在第10行中更改了条件。在此处使用in关键字是更简单易读的方式...

It looks like there may be a problem with the indentation of the first print command, you are printing 'name', but it is outside of the for loop. 看起来第一个打印命令的缩进可能有问题,您正在打印“名称”,但是它在for循环之外。

Try populating your dictionary, and then printing the dictionary, along the lines of: 尝试填充字典,然后按照以下方式打印字典:

with open(your_file) as f:
    found_dic = {}
    key = 'your_key'

    # populate the dictionary
    found_dic[key] = [i for i in f if key in i and i not in found_dic]

With this as a starting point, hopefully you can format the result to the dictionary as you need it. 以此为起点,希望您可以根据需要将结果格式化为字典。 Only lines that include the 'key' will be in the found_dic, so you should be able to print these out in any format you like. found_dic中仅包含“ key”的行,因此您应该能够以自己喜欢的任何格式将其打印出来。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM