首页
> 计算机技术
> 后端开发
> Nim
Nim语言标准库parsecfg对ini配置文件的读写删除操作使用方法
原创 lihf8515于2025年01月24日 21:00发表
来源:本站 阅读:499
由于Nim语言的早期处在不稳定期,各种库不健全,针对ini配置文件的操作不完整,几乎没法读写ini文件,为此我向Nim官方提供代码,完善了 parsecfg 模块,已被Nim官方接纳进标准库中。
当然,现在的Nim语言标准库已经相当完善了。
需要特别注意的是这个模块的缺点是如果ini配置文件中原来有注释语句的话,那么它在保存时会将注释去除,所以,您要是很介意这点的话,我建议您使用我开发的 parseini 模块,它完美解决了此问题。具体使用方法同parsecfg是一样的,可以使用 nimble 命令安装:
nimble install parseini
下面是这个模块的具体使用方法。
import parsecfg
## Creating a configuration file.
var dict1=newConfig()
dict1.setSectionKey("","charset","utf-8")
dict1.setSectionKey("Package","name","hello")
dict1.setSectionKey("Package","--threads","on")
dict1.setSectionKey("Author","name","lihf8515")
dict1.setSectionKey("Author","qq","10214028")
dict1.setSectionKey("Author","email","lihaifeng@wxm.com")
dict1.writeConfig("config.ini")
## Reading a configuration file.
var dict2 = loadConfig("config.ini")
var charset = dict2.getSectionValue("","charset")
var threads = dict2.getSectionValue("Package","--threads")
var pname = dict2.getSectionValue("Package","name")
var name = dict2.getSectionValue("Author","name")
var qq = dict2.getSectionValue("Author","qq")
var email = dict2.getSectionValue("Author","email")
echo charset
echo threads
echo pname
echo name
echo qq
echo email
## Modifying a configuration file.
var dict3 = loadConfig("config.ini")
dict3.setSectionKey("Author","name","lhf")
dict3.writeConfig("config.ini")
## Deleting a section key in a configuration file.
var dict4 = loadConfig("config.ini")
dict4.delSectionKey("Author","email")
dict4.writeConfig("config.ini")
上一篇:python语言的特点
阅读排行榜