Pythonの読み書きと修正my.Iniファイル--Python 3.バージョン0
まずmyを添付します.iniの内容:
[book] title = the python standard library author = fredrik lundh [ematter] pages = 250 [md5] value = kingsoft
iniファイルの読み込み
[book] title = the python standard library author = fredrik lundh [ematter] pages = 250 [md5] value = kingsoft
iniファイルの読み込み
__author__ = 'minggxu9'
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
The ConfigParser module has been renamed to configparser in Python 3.0. The 2to3 tool will automatically
adapt imports when converting your sources to 3.0.
:http://docs.python.org/library/configparser.html
'''
import configparser
config = configparser.ConfigParser()
config.readfp(open('../my.ini')) #
a = config.get("book","author")
print(a)
:
__author__ = 'minggxu9'
import configparser
config = configparser.ConfigParser()
#
config.add_section("book")
config.set("book", "title", "the python standard library")
config.set("book", "author", "fredrik lundh")
config.add_section("ematter")
config.set("ematter", "pages", "250")
#
# write to file
config.write(open('../my.ini', "w"))
#
:
__author__ = 'minggxu9'
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import configparser
config = configparser.ConfigParser()
config.read('../my.ini')
a = config.add_section("md5")
config.set("md5", "value", "1234")
config.write(open('1.ini', "r+")) # r+ , :) r+ , a ,append
:
__author__ = 'minggxu9'
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import configparser
config = configparser.ConfigParser()
config.read('../my.ini')
config.set("md5", "value", "kingsoft") # md5 1234 kingsoft
config.write(open('1.ini', "r+"))
( , )