为什么我的python 2.7代码出现此字符串错误

时间:2018-07-05 14:26:53

标签: python

这是我的代码,我试图将IPLIST的用户输入添加到此代码的中间。我每次都得到无效的语法。我想念什么?我不是真正的程序员,我只是在尝试编写简单的脚本来自动化我的任务:

IPs = raw_input('Enter your IPs:') 

print IPs

payload = "{\"name\":\"madefrompostman\",\"description\":\"\",\"context\":\"\",\"status\":-1,\"createdTime\":0,\"modifiedTime\":0,\"groups\":[],\"repository\":{\"id\":233},\"schedule\":{\"start\":\"TZID=America/New_York:20180705T083000\",\"repeatRule\":\"FREQ=TEMPLATE;INTERVAL=1\",\"type\":\"template\"},\"dhcpTracking\":\"true\",\"emailOnLaunch\":\"false\",\"emailOnFinish\":\"false\",\"reports\":[],\"type\":\"policy\",\"policy\":{\"id\":1002387},\"zone\":{\"id\":-1},\"timeoutAction\":\"import\",\"rolloverType\":\"template\",\"scanningVirtualHosts\":\"false\",\"classifyMitigatedAge\":365,\"assets\":[],\"ipList\":\""**IPs**",\"credentials\":[],\"maxScanTime\":\"unlimited\"}"

1 个答案:

答案 0 :(得分:0)

这不是执行此操作的理想方法,但是您说自己不是程序员。因此,我相信无论您现在还是将来,这种方法都将是最简便的方法。

payload = "{\"name\":\"madefrompostman\",\"description\":\"\",\"context\":\"\",\"status\":-1,\"createdTime\":0,\"modifiedTime\":0,\"groups\":[],\"repository\":{\"id\":233},\"schedule\":{\"start\":\"TZID=America/New_York:20180705T083000\",\"repeatRule\":\"FREQ=TEMPLATE;INTERVAL=1\",\"type\":\"template\"},\"dhcpTracking\":\"true\",\"emailOnLaunch\":\"false\",\"emailOnFinish\":\"false\",\"reports\":[],\"type\":\"policy\",\"policy\":{\"id\":1002387},\"zone\":{\"id\":-1},\"timeoutAction\":\"import\",\"rolloverType\":\"template\",\"scanningVirtualHosts\":\"false\",\"classifyMitigatedAge\":365,\"assets\":[],\"ipList\":\""

payload += IPs

payload += ",\"credentials\":[],\"maxScanTime\":\"unlimited\"}"

发生了什么:

有效负载对象从字符串的开头开始。然后,您的IPs变量被串联(附加)。然后,将字符串的末尾连接起来。

一种较短且可能更好的书写方式

payload = "Beginning of string" + IPs + "end of string."