本文介绍了尝试将文件名写入csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My code determines if the contents of the file returns True or False and output the results to a .csv.

我想将文件名也写到同一行的csv中.

I would like to write the filename also to the csv in the same row.

错误消息

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))

推荐答案

vt_result_file仅作为vt_result_check local 变量存在,您的错误是说此变量不存在于底部文件.

vt_result_file only exists as a local variable to vt_result_check, your error is saying this variable does not exist at the bottom of the file.

此外,(尽管没关系)您在调用创建该变量的函数之前先引用了该变量.

Plus, (even though it doesn't matter) you are referring to that variable before you called the function that creates that variable.

在主功能区域中没有任何要遍历的内容.而您的check函数仅返回一个值.

There is nothing you are looping over in the main function area. And your check function only returns a single value.

因此,您只能写出一个CSV行

Therefore, you can only write out one CSV row

if __name__ == '__main__':
    with open(file_n, 'w') as output:
        writer = csv.writer(output)
        writer.writerow([file_n, vt_result_check(path)])


修改

关于您的评论,您想要这样的东西

Regarding your comment, you want something like this

with open(file_n, 'w') as output:  # Open the CSV file
    writer = csv.writer(output)
    for filename in os.listdir(path):  # Loop over all files to check
        full_filename = path + filename
        with open(full_filename, 'r') as vt_result_file:
            # Load the file and check it
            vt_data = json.load(vt_result_file)
            writer.writerow([full_filename, check_file(full_filename)])

这篇关于尝试将文件名写入csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 13:09
查看更多