开发日记20200314--Python操作Json文件

1需求

  使用Json作为数据源配置文件,需要对Json文件进行增删改查。

2功能设计

2.1配置文件内容

{

    "Configfile": "DBconfig",

    "ConfigNo": "N1",

    "ConfigInfo": [

        {

            "DBName": "MyLocalOra",

            "UserComment": "自己虚拟机oracle服务器,连接dev用户,可读写",

            "DBType": "Oracle",

            "DBEdition": "10g",

            "DBVersion": "12.031",

            "HostName": "localhost",

            "SerName": "ora12c",

            "Port": "1521",

            "UserName": "dev",

            "Password": "dev*cy9"

        },

        {

            "DBName": "LocalOra",

            "UserComment": "本地测试oracle服务器",

            "DBType": "Oracle",

            "DBEdition": "12c",

            "DBVersion": "12.031",

            "HostName": "localhost",

            "SerName": "ora12c",

            "Port": "1521",

            "UserName": "dev",

            "Password": "dev*cy9"

        }

    ]

}

2.1功能

(0)常量定义,这部分以后放到根目录配置文件中

DBConfigFilePath = "D:\Spython\DevlDBMointor\App\ConfigFiles\\"

DBConfigFileName = "DBConfig.json"

DBConfigFileAllName = DBConfigFilePath+DBConfigFileName

(1)读取Json文件中的数据库配置信息,返回Json文件内容列表

def ReadDBConfigInfo(v_file_all_name):

    if os.path.isfile(v_file_all_name):

        t_file_obj = open(v_file_all_name,encoding='utf-8')

        t_json_data = json.load(t_file_obj)

    else:

        print("文件不存在或不可读")

    print(type(t_json_data))

    return t_json_data

(3)编辑DBconfig.Json,v_configinfo_dict包含DBName\UserComment\DBType\DBEdition\DBVersion\HostName\SerName\Port\UserName\PassWord

#v_dbtype 数据库类型,这里默认为Oracle,以后会根据不同数据库连接格式进行补充,过程:在列表中找到需要修改的DBName所在的列表元素,读取网页中的内中逐个更新到列表中,然后将列表更新到字典变量t_dbconfig_dict中,清空DBconfig.json的内容,最后将字典数据写入json文件。

def EditDBConfigInof(v_dbname,v_configinfo_dict,v_dbtype):

    t_dbconfig_dict = ReadDBConfigInfo(DBConfigFileAllName)

    t_dbconfig_list = t_dbconfig_dict['ConfigInfo']

    #修改变更内容

    for t_item in t_dbconfig_list:

        #3.1以下oracle的数据配置

        if t_item['DBName'] == v_dbname and t_item['DBType']==v_dbtype :

            t_item['UserComment'] = v_configinfo_dict['UserComment']

            t_item['DBType'] = v_configinfo_dict['DBType']

            t_item['DBEdition'] = v_configinfo_dict['DBEdition']

            t_item['DBVersion'] = v_configinfo_dict['DBVersion']

            t_item['HostName'] = v_configinfo_dict['HostName']

            t_item['SerName'] = v_configinfo_dict['SerName']

            t_item['Port'] = v_configinfo_dict['Port']

            t_item['UserName'] = v_configinfo_dict['UserName']

            t_item['Password'] = v_configinfo_dict['Password']

        #3.2以下为SQLServer

        if t_item['DBName'] == v_dbname and t_item['DBType']==v_dbtype :

            pass

    t_dbconfig_dict['ConfigInfo'] = t_dbconfig_list

    #清空目标文件

    EmtypeJsonContent(DBConfigFileAllName)

    #将修改后的数据写入文件

    AppendJsonContent(DBConfigFileAllName, t_dbconfig_dict)

(7)判断用户输入的数据链接名是否存在,存在返回True ,不存在返回False

def DBnameIsExists(v_dbname):

    t_dbconfig_list = ReadDBConfigInfo(DBConfigFileAllName)

    for t_item in t_dbconfig_list:

        if t_item['DBName'] == v_dbname :

            return True

        print(t_item)

    print(t_dbconfig_list)

    return False

(8)清空json文件内容

def EmtypeJsonContent(v_file_all_name):

    if os.path.isfile(v_file_all_name):

        file_obj = open(v_file_all_name, encoding='utf-8', mode='w')

        file_obj.truncate()

        file_obj.close

        print(v_file_all_name + "文件内容成功被清空。")

    else:

        print("文件不存在,请重新选择文件")

(9)在空json文件中写入内容

def AppendJsonContent(v_file_all_name,v_content_dict):

    file_obj = open(v_file_all_name, mode="w", encoding="utf-8")

    file_obj.write(json.dumps(v_content_dict, ensure_ascii=False,indent=4))

(10)单元测试

def UnitTest():

    configinfo_dict =    {

      "DBName": "MyLocalOra",

      "UserComment": "自己虚拟机oracle服务器,连接dev用户,可读写",

      "DBType": "Oracle",

      "DBEdition": "10g",

      "DBVersion": "12.031",

      "HostName": "localhost",

      "SerName": "ora12c",

      "Port": "1521",

      "UserName": "dev",

      "Password": "dev*cy9"

    }

    EditDBConfigInof("MyLocalOra", configinfo_dict, "Oracle")

if __name__ == '__main__':

    UnitTest()

你可能感兴趣的:(开发日记20200314--Python操作Json文件)