我有一个python脚本,在这个脚本中,我试图读取目录中的所有.txt文件,并确定它们对于脚本中的任何条件是否返回True或False。我有数千个.txt文件,其中有.json格式的文本。但是,我收到一条错误消息,说invalid.json格式。我已经检查了我的文本文件是否为.json格式。我希望脚本确定.txt文件是否与下面代码中的任何语句匹配。然后我想将结果输出到csv文件。非常感谢你的帮助!我已经包括了我的错误消息和example.txt文件。在
带有.json格式的Example.txt文件{
"domain_siblings": [
"try.wisebuygroup.com.au",
"www.wisebuygroup.com.au"
],
"resolutions": [
{
"ip_address": "34.238.73.135",
"last_resolved": "2018-04-22 17:59:05"
},
{
"ip_address": "52.0.100.49",
"last_resolved": "2018-06-24 17:05:06"
},
{
"ip_address": "52.204.226.220",
"last_resolved": "2018-04-22 17:59:06"
},
{
"ip_address": "52.22.224.230",
"last_resolved": "2018-06-24 17:05:06"
}
],
"response_code": 1,
"verbose_msg": "Domain found in dataset",
"whois": null
}
错误消息
^{pr2}$
编码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))