我有一个python脚本,在这个脚本中,我试图确定是否以json格式的.txt文件返回包含某些语句的正或负。一些例子陈述是:“高曝光”、“网络钓鱼和其他欺诈”、“可疑内容”。在确定每个.txt文件是否返回正或负后,脚本应该将结果写入csv。我正试图处理大约10万个.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)):发布于 2018-08-17 13:09:35
将vt_result转换为字符串:
vt_result = str(vt_result)在内部循环的第一次迭代中,这不是问题,但在第二次迭代中,值没有重置,您尝试对字符串("True“或"False")和失败的布尔值执行|=。
可以通过移动
vt_result = False下面
for filename in os.listdir(path):如果由于需要继续使用以前迭代中的值,这是不可执行的,只需删除转换行:print可以很好地打印布尔值。
发布于 2018-08-17 13:09:43
错误表明,在给定的行中,|=操作符两边的值对于该操作来说都是不兼容的类型;一个是str,另一个是bool。|=后面的表达式应该总是计算到bool,因此您需要找到vt_result变成str的位置。你明显地把它弄得更深了一点:
vt_result = str(vt_result)因此,下一次通过for循环之后,当您第二次运行vt_result |= ...行时,就会得到错误。
https://stackoverflow.com/questions/51895839
复制相似问题