开发日记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()

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,844评论 0 13
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 3,053评论 0 3
  • 背景: 阅读新闻 12C CDB模式下RMAN备份与恢复 [日期:2016-11-29] 来源:Linux社区 作...
    阳屯okyepd阅读 3,807评论 0 7
  • 本学习笔记针对有其他语言基础的情况下记录的, 主要记录一些与其他语言不一样的地方, 使用于快速学习. 常用指令 p...
    GrayLand阅读 1,162评论 0 3
  • 常用模块 认识模块 什么是模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文...
    go以恒阅读 2,143评论 0 6

友情链接更多精彩内容