Python请求体自动添加单引号

2024-09-28 04:56:20 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的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循环是如何修改或编码有效负载的,从而产生这个问题的?你的帮助将不胜感激。你知道吗


Tags: orghttpurlfordomainitemlistpayload
1条回答
网友
1楼 · 发布于 2024-09-28 04:56:20

您的代码:

for item in domain_list_formatted_tuple:
   printcorrectly = ' '.join(map(str, (domain_list_formatted_tuple)))   
   payload["threatInfo"]["threatEntries"] = [printcorrectly]

应该是:

for item in domain_list_formatted_tuple:
   printcorrectly = ' '.join(map(str, (domain_list_formatted_tuple)))   
   payload["threatInfo"]["threatEntries"] = printcorrectly

没有括号在printcorrectly

如果您有:

a = ['xxx']
print(a)

您将得到带有括号和引号的输出['xxx']。你知道吗

相关问题 更多 >

    热门问题