苗翠花360云盘:Python 简洁ini读写 - 代码分享 - 开源中国社区
来源:百度文库 编辑:九乡新闻网 时间:2024/05/05 02:49:05
Python 简洁ini读写Sephiroth 发布于 2010年11月15日 20时 (0评) 1人收藏此代码, 我要收藏(?) 标签: INI
代码片段(1)
[代码] [Python]代码
view sourceprint?01
#coding=utf-8
02
03
import
ConfigParser
04
05
def
writeConfig(filename):
06
config
=
ConfigParser.ConfigParser()
07
# set db
08
section_name
=
'db'
09
config.add_section( section_name )
10
config.
set
( section_name,
'dbname'
,
'MySQL'
)
11
config.
set
( section_name,
'host'
,
'127.0.0.1'
)
12
config.
set
( section_name,
'port'
,
'80'
)
13
config.
set
( section_name,
'password'
,
'123456'
)
14
config.
set
( section_name,
'databasename'
,
'test'
)
15
16
# set app
17
section_name
=
'app'
18
config.add_section( section_name )
19
config.
set
( section_name,
'loggerapp'
,
'192.168.20.2'
)
20
config.
set
( section_name,
'reportapp'
,
'192.168.20.3'
)
21
22
# write to file
23
config.write(
open
(filename,
'a'
) )
24
25
def
updateConfig(filename, section,
*
*
keyv):
26
config
=
ConfigParser.ConfigParser()
27
config.read(filename)
28
[config.
set
(section, key, keyv[key])
for
key
in
keyv
if
config.has_option(section, key)]
29
config.write(
open
(filename,
'r+'
) )
30
31
if
__name__
=
=
'__main__'
:
32
file_name
=
'test.ini'
33
writeConfig(file_name)
34
updateConfig(file_name,
'app'
, reportapp
=
'192.168.100.100'
)
35
print
"end__"
36
37
#coding=utf-8
38
39
import
ConfigParser
40
41
def
writeConfig(filename):
42
config
=
ConfigParser.ConfigParser()
43
# set db
44
section_name
=
'db'
45
config.add_section( section_name )
46
config.
set
( section_name,
'dbname'
,
'MySQL'
)
47
config.
set
( section_name,
'host'
,
'127.0.0.1'
)
48
config.
set
( section_name,
'port'
,
'80'
)
49
config.
set
( section_name,
'password'
,
'123456'
)
50
config.
set
( section_name,
'databasename'
,
'test'
)
51
52
# set app
53
section_name
=
'app'
54
config.add_section( section_name )
55
config.
set
( section_name,
'loggerapp'
,
'192.168.20.2'
)
56
config.
set
( section_name,
'reportapp'
,
'192.168.20.3'
)
57
58
# write to file
59
config.write(
open
(filename,
'a'
) )
60
61
def
updateConfig(filename, section,
*
*
keyv):
62
config
=
ConfigParser.ConfigParser()
63
config.read(filename)
64
[config.
set
(section, key, keyv[key])
for
key
in
keyv
if
config.has_option(section, key)]
65
config.write(
open
(filename,
'r+'
) )
66
67
if
__name__
=
=
'__main__'
:
68
file_name
=
'test.ini'
69
writeConfig(file_name)
70
updateConfig(file_name,
'app'
, reportapp
=
'192.168.100.100'
)
71
print
"end__"
Python 简洁ini读写 - 代码分享 - 开源中国社区
Python 常用代码片段 - 代码分享 - 开源中国社区
Python 修改MP3 - 代码分享 - 开源中国社区
Python threading模块 - 代码分享 - 开源中国社区
Python 分割TXT文件成4K的TXT文件 - 代码分享 - 开源中国社区
wxGlade - Python的可视化编程工具 - 开源中国社区
Python 操作 ini文件
vb 读写ini文件
新浪读书频道下载合成txt小说(修改) - 代码分享 - 开源中国社区
关于python的'版本选取+开发工具+扩展'的问题 - 开源中国社区
分享 7 大开源微博客程序【图文】 - 开源中国社区
转载:看看代码读写了哪些地址
【时钟】简洁红色日历+喜气洋洋月历代码
简洁红色日历+喜气洋洋月历代码
Python
python
上传和管理 Python 应用程序 - Google App Engine - Google 代码
Python 代码风格 和 PEP8 - 不过而而 - BlogJava
【转】我的nandflash读写源码,代码清晰
编程中国?>>?Python成为2010年度语言
美女Flash代码分享
分享导航模块代码
模块代码分享
图片素材仓库 分享代码