python阅读ini文件

1.创建一个ini文件
[default]
age=12
name="tea"

[account01]
account=test001
pwd=001

[account02]
account=test002
pwd=002

  2.读取ini文件

# Read ini configure infomation from ini file.
cf = configparser.ConfigParser()
cwd = os.path.split(os.path.realpath(__file__))[0]
filename = cwd + os.path.sep + '..' + os.path.sep + 'test.ini'
cf.read(filename, encoding='utf8')
self.__dict__.update({item[0]: item[1] for item in cf.items('account02')})

#使用数据:
print(self.account) #test002
print(self.pwd) #002

 



posted @ 2025-01-23 18:17  苹果芒  阅读(5)  评论(0)    收藏  举报