在我的python脚本中,我尝试遍历一个包含域名的文本文件,并将它们填充到我的JSON请求主体中。API调用所需的正确格式为
payload = {
"threatInfo": {
"threatEntries": [
{"url": "http://malware.wicar.org/"}, {"url": "http://urltocheck2.org"},
]
}
}
我用来复制这个的变量叫做mystring
domain_list_formatted = []
for item in domain_list:
domain_list_formatted.append("""{"url": """ + '"{}"'.format(item) + "},")
domain_list_formatted_tuple= tuple(domain_list_formatted)
mystring = ' '.join(map(str, (domain_list_formatted_tuple)))
打印mystring会得到需要传递给payload变量的结果
{"url": "http://malware.wicar.org/"},
{"url": "http://www.urltocheck2.org/"},
但是,我想循环这个,所以我添加了以下循环
for item in domain_list_formatted_tuple:
printcorrectly = ' '.join(map(str, (domain_list_formatted_tuple)))
payload["threatInfo"]["threatEntries"] = [printcorrectly]
结果是:
['{"url": "http://malware.wicar.org/"}, {"url": "http://www.urltocheck2.org/"}']
括号外的单引号将其完全去掉。for循环是如何修改或编码有效负载的,从而产生这个问题的?你的帮助将不胜感激。你知道吗
您的代码:
应该是:
没有括号在
printcorrectly
如果您有:
您将得到带有括号和引号的输出
['xxx']
。你知道吗相关问题 更多 >
编程相关推荐