Memoru

System Engineering and Programming and IT

json.dump

json_sample.py

#! /usr/bin/env python
"""{fname}
# sample code for csv
Usage:
"""

__author__ = 'Memoru'
__version__ = '0.0.1'
__date__ = '2018-06-26'

import sys
import os
__doc__ = __doc__.format(fname=os.path.basename(__file__))

import json
from pathlib import Path

import logging.config
import configbase as base
CONF = base.CONF
ENV = base.ENV



#  Debug ---------------------------------------  #

def process(args={'json_file' : 'sample.json'}):
    try:
        logger = logging.getLogger(__name__)
        logger.debug('◇◇--Begin sample -----------------◇◇')
        logger.debug(__doc__)
        logger.debug(f'CONF.MEDIA_ROOT={CONF.MEDIA_ROOT}')
        csv_root = CONF.MEDIA_ROOT
        p = Path(csv_root)
        jsonf = args['json_file']
        built_p = p / jsonf
        logger.debug(built_p)
        
        obj = { 'id' : '123456789_1',
                'contact_id' : '123456789',
                'c_no' : '1',
                'top_class' : '挨拶',
                'text' : 'こんにちは'
              }
        f = open(built_p, 'w', encoding='utf-8')
        json.dump(obj, f ,ensure_ascii=False, indent=4)
        
        
        logger.debug('◆◆--End   sample -----------------◆◆')
    except Exception as ex:
        logger.exception(ex)
        raise ex
        

if __name__ == '__main__':
    logging.config.fileConfig('logging_debug.conf')
    #process()
    args = {'json_file' : 'sample2.json'}
    process(args)