首页 > 解决方案 > 试图将文件名写入 csv

问题描述

我的代码确定文件的内容是否返回TrueFalse将结果输出到 .csv。

我想将文件名也写入同一行的 csv 中。

错误信息

for i in range(vt_result_file):
NameError: name 'vt_result_file' is not defined

代码

import os
import json
import csv

path=r'./output/'
csvpath='C:/Users/xxx/Documents/csvtest'
file_n = 'file.csv'

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 str(vt_result)


if __name__ == '__main__':
    with open(file_n, 'w') as output:
        for i in range(vt_result_file):
            output.write(vt_result_file, vt_result_check(path))

标签: pythonjsonpython-3.xcsv

解决方案


您收到错误是因为您尝试使用vt_result_file不在范围内的变量 —— 它在您尝试访问它的脚本部分中不存在。您正在尝试使用它在这里,大概是循环一组文件:

    for i in range(vt_result_file):
        output.write(vt_result_file, vt_result_check(path))

vt_result_file仅存在于vt_result_check函数中,当您尝试在for i in range(vt_result_file).

您也在重复工作,因为您的vt_result_check函数会遍历目录中的所有文件,因此您无需执行相同的操作即可获得结果。

看起来您的主要功能也无法正常工作,因为您正在遍历文件,将内容设置为vt_data,但您只对最后一组数据进行了进一步分析:

    with open(path + filename, 'r') as vt_result_file:
        # this is done for every file
        vt_data = json.load(vt_result_file)

    # check the indentation level of the code
    # it'll only run on the last value of vt_data
    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, []))

我想你可能想在每个文件上运行分析代码,然后保存每个文件的结果。一种简单的方法是使用字典 - 请参阅https://docs.python.org/3/library/stdtypes.html#mapping-types-dict 以下是有关如何重组代码的建议:

def vt_result_check(path):

    # initialise an empty dictionary for your results
    results = {}

    sample_types = ('detected_referrer_samples', 'detected_communicating_samples',
                            'detected_downloaded_samples', 'detected_urls')
    threats = ("elevated exposure", "phishing and other frauds", "suspicious content")

    for filename in os.listdir(path):
        with open(path + filename, 'r') as vt_result_file:
            # set this to false for each file
            vt_result = False
            vt_data = json.load(vt_result_file)

            # do all your analysis here
            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
            vt_result |= vt_data.get('Forcepoint ThreatSeeker category') in threats

            # save the file name and the result in your dict
            results[vt_result_file] = str(vt_result)

    # return the dictionary
    return results

然后,您可以按如下方式打印结果:

if __name__ == '__main__':
    result_dict = vt_result_check(path)
    with open(file_n, 'w') as output:
        for i in result_dict:
            output.write(i + ": " + result_dict[i])

推荐阅读