2017-03-23 11 views
0

(編集済み、前のWAAAY) - forループ内で文字列substを動かすことで機能するようになりましたが、結果は自動的に5つのタブを私は...私は誰もが答えを持っているかどうかを確認するために少しのためにそれを残したかった複数行の中でのPythonリストの変数置換...

を取り除くために、多分私を次の誰かを助けるように見えることはできませんことを左

コード:

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()) 

(前) 私はリストを持っています私は入れません

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)リスト(dns_list)を反復処理し、複数行の文字列に変数 'の挿入' を配置する必要があり alert_dns.format(I) KeyError例外: '挿入'

+0

どこに文字列を挿入する必要がありますか? "insert"を置き換える必要があります – Glacier11

+0

はい、私は、書式機能を使って挿入物をリストの内容に置き換えることができると思いました。 –

答えて

1

代わりのalert_dns.format(i)

あなたがこれはANS

alert_dns.format(insert=i) 
+0

Thanks @ anthony-kongは、複数行の文字列 "value":{insert} "の可変位置に 'insert'を入れますが、リストの値は入れません。 –

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') 
関連する問題