多行字符串中的Python列表变量替换...几乎(已编辑)

时间:2017-03-23 23:00:42

标签: python format

(编辑,上面的WAAAY) - 我通过在for循环中移动字符串subst来使它工作,但结果会自动将5个制表符放在左边,我似乎无法摆脱

我想稍微留下一点,看看是否有人有答案,也许是为了帮助跟随我的人......

代码:

for i in dns_list:
    with open("output.txt", "a") as output:
        alert_dns = textwrap.dedent("""
                    { 
                       \"tests\":[  
                           {{  
                             \"token\":\"DNS\",
                             \"type\":\"text\",
                             \"operator\":\"contains\",
                             \"preservecase\":false,
                             \"value\":\"%s\"
                          }
                       ]
                    """)%(i)
        alert_dns=alert_dns.strip()
        output.write(alert_dns.strip())

(前一) 我有一个域名列表,我需要遍历列表(dns_list)并将变量'insert'放入多行字符串(alert_dns) -

alert_dns="""
{{  
 \"tests\":[  
     {{  
       \"token\":\"DNS\",
       \"type\":\"text\",
       \"operator\":\"contains\",
       \"preservecase\":false,
       \"value\":\"{insert}\"
    }}
 ]
}}
"""

dns_list=[]
temp_file_name = 'daily.csv'
with open(temp_file_name, 'r') as temp_file:
    lines = temp_file.read()
    dns = re.findall(urlmarker.WEB_URL_REGEX,lines)
    for i in dns:
        dns_list.append(i)

with open("output.txt", "w") as output:
    for i in dns_list:
        for insert in alert_dns:
            # i=insert
            alert_dns.format(i)
            output.write(alert_dns+'\n')

我一直在 -     alert_dns.format(ⅰ) KeyError:'insert'

2 个答案:

答案 0 :(得分:1)

而不是build

你应该

chmod 777

答案 1 :(得分:0)

好吧,如果有人找到它,这就是答案......

with open("output.txt", "w") as output:
    for i in dns_list:
        alert_dns = textwrap.dedent("""\
                    { 
                       \"tests\":[  
                           {{  
                             \"token\":\"DNS\",
                             \"type\":\"text\",
                             \"operator\":\"contains\",
                             \"preservecase\":false,
                             \"value\":\"%s\"
                          }
                       ]
                    """)%(i)
        output.write(alert_dns+'\n')