python模块-configparse

概述

学习一下configparse的用法。其实很简单,就是记录一下代码。方便查找

代码

config.ini

1
2
3
4
5
6
[head]
title='Hello World'

[body]
div="this is world"
h1 = "this is h1"

config.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env python 
#-*- coding:utf-8 -*-

import configparser

config = configparser.ConfigParser()
config.read('config.ini')

sections = config.sections()
print(sections) # [head, body]

head = config['head']
print(head) # <Section: head>

title = head['title']
print(title) # 'Hello World'

body = config['body']
print(body) # <Section: body>

for k,v in body.items():
print(k,v)

# div "this is world"
# h1 "this is h1"