#!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import json import mysql.connector with open ('/webspace/path/info.json', 'w') as f: cnx = mysql.connector.connect(user='user', password="pass", database='db') cursor = cnx.cursor() cursor.execute("SELECT node FROM caps_features GROUP BY subnode;") result = dict() for client in cursor: client = client[0] if client not in result.keys(): result[client] = 1 else: result[client] += 1 cursor.close() cnx.close() try: f.write(json.dumps(result, indent=4, sort_keys=True)) except FileNotFoundError: pass f.close()