【发布时间】:2018-08-17 12:50:33
【问题描述】:
我有一个 python 脚本,我试图确定 json 格式的 .txt 文件是否返回正数或负数以包含某些语句。一些示例语句是:“高曝光率”、“网络钓鱼和其他欺诈”、“可疑内容”。在确定每个 .txt 文件返回正数还是负数后,脚本应该将结果写入 csv。我正在尝试处理大约 100,000 个 .txt 文件。我得到了我的代码的TypeError on line 22。完整的错误信息是:
TypeError: unsupported operand type(s) for |=: 'str' and 'bool' message when trying to run script
我在下面包含了我的代码和 json 格式的示例 .txt 文件。
示例 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
}
完整追溯
Traceback (most recent call last):
File "C:/virustotal_reporter.py", line 47, in <module>
vt_result_check(path)
File "C:/virustotal_reporter.py", line 22, in vt_result_check
vt_result |= any(sample['positives'] > 0 for sample_type in sample_types
TypeError: unsupported operand type(s) for |=: 'str' and 'bool'
代码
import os
import json
import csv
path="C:/Users/bwerner/Documents/output/"
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)
l = ()
# 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
vt_result = str(vt_result)
print(vt_result)
# with open('output.csv', 'w') as outfile:
# outfile.write(vt_result)
# print(vt_result_check(path))
#f.writerow(vt_result_check(path))
# l.append(vt_result)
return vt_result
if __name__ == '__main__':
vt_result_check(path)
# for i in range(vt_result_check(path)):
【问题讨论】:
-
能否包含异常的完整回溯?
-
@glibdud 感谢您的评论!我刚刚添加了完整的 Traceback。非常感谢您的帮助!
-
你在循环结束时做
vt_result = str(vt_result),所以当下一个循环开始时vt_result是一个字符串。我怀疑您可能想在循环内移动vt_result = False行。
标签: python json python-3.x