简体   繁体   English

如何使python脚本正常工作以从.txt文件中获取输入并返回肯定数

[英]How to get python script properly working to take input from .txt files and return the number of positives

I have a python script that is supposed to take a directory full of .txt files and determine if each .txt file return positive or negative for matching certain text statements inside the file itself like "known infection source". 我有一个python脚本,应该使用充满.txt文件的目录并确定每个.txt文件返回的是正数还是负数,以匹配文件内部的某些文本语句,例如“已知感染源”。 However, my script doesn't work and returns the following error message. 但是,我的脚本不起作用,并返回以下错误消息。 Any help would be greatly appreciated! 任何帮助将不胜感激!

Sample JSON file text 样本JSON文件文本

{
    "detected_referrer_samples": [
        {
            "positives": 1,
            "sha256": "325f928105efb4c227be1a83fb3d0634ec5903bdfce2c3580ad113fc0f15373c",
            "total": 52
        },
        {
            "positives": 20,
            "sha256": "48d85943ea9cdd1e480d73556e94d8438c1b2a8a30238dff2c52dd7f5c047435",
            "total": 53
        }
    ],
    "detected_urls": [],
    "domain_siblings": [],
    "resolutions": [],
    "response_code": 1,
    "verbose_msg": "Domain found in dataset",
    "whois": null
}

Error 错误

Traceback (most recent call last):
  File "vt_reporter1.py", line 35, in <module>
    print(vt_result_check(path))
  File "vt_reporter1.py", line 20, in vt_result_check
    vt_result |= any(sample['positives'] > 0 for sample_type in sample_types
  File "vt_reporter1.py", line 21, in <genexpr>
    for sample in vt_data.get(sample_type, []))
AttributeError: 'list' object has no attribute 'get'

Code

import os
import json
import csv

path=r'./output/'
csvpath='C:/Users/bwerner/Documents'

def vt_result_check(path):
    vt_result = False
    for filename in os.listdir(path):
        with open(path + filename, 'r') as vt_result_file:
            vt_data = json.load(vt_result_file)

        # Look for any positive detected referrer samples
        # Look for any positive detected communicating samples
        # Look for any positive detected downloaded samples
        # Look for any positive detected URLs
        sample_types = ('detected_referrer_samples', 'detected_communicating_samples',
                        'detected_downloaded_samples', 'detected_urls')
        vt_result |= any(sample['positives'] > 0 for sample_type in sample_types
                                                 for sample in vt_data.get(sample_type, []))

        # Look for a Dr. Web category of known infection source
        vt_result |= vt_data.get('Dr.Web category') == "known infection source"

        # Look for a Forecepoint ThreatSeeker category of elevated exposure
        # Look for a Forecepoint ThreatSeeker category of phishing and other frauds
        # Look for a Forecepoint ThreatSeeker category of suspicious content
        threats = ("elevated exposure", "phishing and other frauds", "suspicious content")
        vt_result |= vt_data.get('Forcepoint ThreatSeeker category') in threats

    return vt_result

if __name__ == '__main__':
    print(vt_result_check(path))
    with open(csvpath, 'w') as csvfile:
        writer.writerow([vt_result_check(path)])

The error tells you everything you need to know about what is going wrong, which is that you cannot call the get() function on a list. 该错误告诉您所有有关发生问题的信息,即您无法在列表上调用get()函数。 In Python, the get() function can only be used with dictionaries, which are different from lists. 在Python中,get()函数只能与字典使用,字典与列表不同。 Instead of using the get() function, call a specific index of the list and your program should work. 而不是使用get()函数,而是调用列表的特定索引,您的程序应该可以工作。 For example: 例如:

for sample in list[10:11]

which returns the 11th element of the list. 它返回列表的第11个元素。

Can you post the contents of the file, or some text that represents the content of the file that is being read in? 您可以发布文件的内容,还是一些表示正在读取的文件内容的文本?

Here's some feedback based on what is seen in the code you posted: 以下是一些根据您发布的代码中看到的反馈:

  1. vt_result_file contains valid JSON vt_result_file包含有效的JSON
  2. And whatever it is reading, is being read into python as a List. 无论读取什么,都将其作为List读入python。 We can determine this because of the error that you are receiving. 由于您收到的错误,我们可以确定这一点。 Look at the last line of the error: 查看错误的最后一行:

    AttributeError: 'list' object has no attribute 'get' AttributeError:“列表”对象没有属性“获取”

    It says that you are trying to access the "get" attribute on a "list". 它表示您正在尝试访问“列表”上的“ get”属性。 Looking at your code, we can see that you are calling "get" on "vt_data" three times: 查看您的代码,我们可以看到您在“ vt_data”上调用了三次“ get”:

    • Once as 曾经
       vt_data.get(sample_type, []) vt_data.get(sample_type,[]) 
    • Another time as 另一次
       vt_data.get('Dr.Web category') vt_data.get('Dr.Web category') 
    • And finally as 最后作为
       vt_data.get('Forcepoint ThreatSeeker category') vt_data.get('Forcepoint ThreatSeeker类别') 

    Per the error message, your variable vt_data is a list and not a dictionary. 根据错误消息,变量vt_data是列表而不是字典。

So you need to ask yourself: 因此,您需要问自己:

Were you expecting vt_result_file to contain a dictionary? 您是否期望vt_result_file包含字典? If so, open the file and examine what is contained there, and turn it into a dictionary. 如果是这样,请打开文件并检查其中包含的内容,然后将其变成字典。

Unfortunately, without seeing the contents of this file, it is hard to suggest what you need to change to fix this error. 不幸的是,在没有看到此文件的内容的情况下,很难建议您需要进行哪些更改以修复此错误。

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

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