Python循环会覆盖之前写入json文件的文本

时间:2018-07-20 13:58:13

标签: python sql json python-3.x

我有一个python脚本,该脚本执行sql查询并将查询的输出写入.json文件。但是,每次为我写入json文件时,它都会覆盖先前写入的文本。我希望将每个sql查询写入新的单独的.json。下面是我的代码不起作用。任何帮助将不胜感激!

from __future__ import print_function

try:
    import psycopg2
except ImportError:
    raise ImportError('\n\033[33mpsycopg2 library missing. pip install psycopg2\033[1;m\n')
    sys.exit(1)

import re
import sys
import json

DB_HOST = 'crt.sh'
DB_NAME = 'certwatch'
DB_USER = 'guest'
OUTPUT_DIR="output/"

def connect_to_db(domain_name):
    try:
        conn = psycopg2.connect("dbname={0} user={1} host={2}".format(DB_NAME, DB_USER, DB_HOST))
        cursor = conn.cursor()
        cursor.execute("SELECT ci.NAME_VALUE NAME_VALUE FROM certificate_identity ci WHERE ci.NAME_TYPE = 'dNSName' AND reverse(lower(ci.NAME_VALUE)) LIKE reverse(lower('%{}'));".format(domain_name))
    except:
        print("\n\033[1;31m[!] Unable to connect to the database\n\033[1;m")
    return cursor


def get_unique_emails(cursor, domain_name):
    unique_emails = []
    for result in cursor.fetchall():
        matches=re.findall(r"\'(.+?)\'",str(result))
        for email in matches:
            if email not in unique_emails:
                if "{}".format(domain_name) in email:
                    unique_emails.append(email)
    return unique_emails


def print_unique_emails(unique_emails):
    print("\033[1;32m[+] Total unique emails found: {}\033[1;m".format(len(unique_emails)))
    for unique_email in sorted(unique_emails):
        print(unique_email)


if __name__ == '__main__':
    filepath = 'test.txt'
    with open(filepath) as fp:
        for cnt, domain_name in enumerate(fp):
            print("Line {}: {}".format(cnt, domain_name))
            print(domain_name)

        domain_name = domain_name.rstrip()
        cursor = connect_to_db(domain_name)
        unique_emails = get_unique_emails(cursor, domain_name)
        print_unique_emails(unique_emails)
        outfilepath = OUTPUT_DIR + unique_emails + ".json"
        with open(outfilepath, 'w') as outfile:
            outfile.write(json.dumps(unique_emails, sort_keys=True, indent=4))

2 个答案:

答案 0 :(得分:2)

with open(outfilepath, 'w') as outfile:
    outfile.write(json.dumps(unique_emails, sort_keys=True, indent=4))

您当前正在打开要写入的文件。您要追加到文件。您可以通过将w更改为a

来完成此操作
with open(outfilepath, 'a') as outfile:
    outfile.write(json.dumps(unique_emails, sort_keys=True, indent=4))

您可以阅读open() here上的文档。

答案 1 :(得分:1)

我认为这是因为在编写json文件时您没有循环,因此只有一次写入,因此它只写入一个文件。因此,您需要做类似... enumerate(fp):的操作。进行另一个for循环,遍历每个域,然后将OUTPUT_DIR + unique_emails +“ .json”更改为OUTPUT_DIR + domain_name +“ .json”。