python的ini文件如何写注释

[env_info]
host_url = https://testenv.test.com
account=zhangdan
pwd=zhangd
description=testinfo

上面是一个ini文件格式的内容:包括有节点env_info,还有节点里面的键值对数据。如果想注释掉其中的某行键值对数据怎么办呢,有2种注释方法:

第一种:使用# 来注释,这个跟python代码注释方式一样的。

[env_info]
host_url = https://testenv.test.com
account=zhangdan
pwd=zhangd
#description=testinfo

 第二种:使用; 分号来注释;

[env_info]
host_url = https://testenv.test.com
account=zhangdan
pwd=zhangd
;description=testinfo

以上两种注释方式都是有效的,可以用代码来验证是否注释成功:

创建一个test.py文件,内容如下:

import configparser
import os

cf = configparser.ConfigParser()
cwd = os.path.split(os.path.realpath(__file__))[0]
filename = cwd + os.path.sep + 'resouce.ini'
cf.read(filename, encoding='utf8')
cf.items("env_info")

就只会打印出:

[('host_url' ,'https://testenv.test.com'),('account','zhangdan'),('pwd','zhangd')]

posted @ 2023-03-16 11:12  苹果芒  阅读(946)  评论(0编辑  收藏  举报