import json import os import pymysql import re kv = {} response_file_path = r"E:/yuxin/cmip20240424/tas/mon" if __name__ == "__main__": db = pymysql.connect(host='47.113.231.200', port=28089, user='root', passwd='passok123A', db='jd_data', charset='utf8mb4') cursor = db.cursor() count = 0 date_pattern = r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}" for file_name in os.listdir(response_file_path): response_file = open("{}/{}".format(response_file_path, file_name)) response_file_content = response_file.read() response_file.close() response_data_body = json.loads(response_file_content) docs = response_data_body['response']['docs'] for doc in docs: cols = [] vals = [] count += 1 for key in doc: val = doc[key] cols.append(key) if type(val) == str: if re.match(date_pattern, val): val = val.replace("T", " ").replace("Z", "") vals.append("'{}'".format(val)) elif type(val) == list: if len(val) == 1: list_val = val[0] else: list_val = json.dumps(val, ensure_ascii=False) list_val = list_val.replace("'", "\\'") vals.append("'{}'".format(list_val)) else: vals.append("{}".format(val)) SQL_INSERT = "INSERT INTO jd_data.cmip6_search_result ({}) VALUES ({})".format(", ".join(cols), ", ".join(vals)) cursor.execute(SQL_INSERT) print("[No. {}]".format(count)) db.commit() db.close()