首页 > 解决方案 > 如何读取 .cfg 文件中的内容并将其解析成字典?

问题描述

如何读取 .cfg 文件中的内容并将其解析成字典?

配置如下:

uid = reader
pwd = xxxxxx
driver =MySQL
port = 43306
db = xxxxxx
ip = 192.168.xxx.xxx

标签: dolphindb

解决方案


自定义函数,读取 CFG 文件并将其转换为字典。

def getMysqlConnDict() {
    f = file("/data/software/dolphin/server/mysqlconn.cfg")
    arr = f.readLines()
    
    connDict = dict(STRING, ANY)
    for(i in 0:arr.size()) {
        connDict[trim(split(arr[i], "=")[0])] = trim(split(arr[i], "=")[1])
    }
    return connDict
}

connDict = getMysqlConnDict()

推荐阅读