The configuration file (.ini) has two formats:
session
1.key=value
2.key:value
Create an ini file test, the content is as follows:
[jingdong]
loca=beijing
boss=liuqiangdong
[Alibaba]
loca=hangzhou
boss=mayun
[tecent]
loca=shehzhen
boss=mahuateng
[huawei]
loca=shenzhen
boss=renzhenfei
1.read() function
def read(self, filenames, encoding=None):
# Read and parse a filename or an iterable of filenames.
# Return list of successfully read files.
Use read() to read the file and traverse the section:
import configparser
file_path = "E:/program/python/test/test1/test.ini"
config = configparser.ConfigParser()
config.read(file_path, encoding="gbk")
for x in config:
print(x)
# DEFAULT
# jingdong
# Alibaba
# tecent
# huawei
2.item() function
def items(self, section=_UNSET, raw=False, vars=None):
"""Return a list of (name, value) tuples for each option in a section.
1) Get all the options of a section
t=config.items("huawei")
print(t)
# [('loca', 'shenzhen'), ('boss', 'renzhenfei')]
2) Because it is presented as a list, it can of course be traversed
for x in config.items("huawei"):
print(x)
# ('loca', 'shenzhen')
# ('boss', 'renzhenfei')
3.add_section() function
def add_section(self, section):
"""Create a new section in the configuration.
Add a new section: "dajiang"
config.add_section("dajiang")
for x in config:
print(x)
# DEFAULT
# jingdong
# Alibaba
# tecent
# huawei
# dajiang
However, "dajiang" does not have options. The function below can be added with the
4.set() function to set each option (add and modify)
def set(self, section, option, value=None):
"""Set an option."""
New)
config.add_section("dajiang")
config.set("dajiang","chanpin","wurenji")
print(config.items("dajiang"))
# [('chanpin', 'wurenji')]
modify)
config.set("dajiang","chanpin","hangtianfeiji")
print(config.items("dajiang"))
#[('chanpin', 'hangtianfeiji')]
5.get() function
def get(self, section, option, *, raw=False, vars=None, fallback=_UNSET):
"""Get an option value for a given
Get the value of option under the section given by section
print(config.get("huawei","boss"))
#renzhengfei
You can also get the elements after the equal sign of an option like this
print(config['huawei']['boss'])
#renzhenfei
6. When a section is added, it will be deleted. The pop() here is not the last element of the regular delete list, and it is deleted according to the section name
t=config.pop("huawei")
print(t)
for x in config:
print(x)
# <Section: huawei>
# DEFAULT
# jingdong
# Alibaba
# tecent
# dajiang
But he has no "memory", huawei was deleted before, and it reappears after traversing again.
t=config.pop("jingdong")
print(t)
for x in config:
print(x)
# <Section: jingdong>
# DEFAULT
# Alibaba
# tecent
# huawei
# dajiang
One last complete traversal:
t=config.pop("jingdong")
print(t)
for x in config:
print(config.items(x))
# <Section: jingdong>
# []
# [('loca', 'hangzhou'), ('boss', 'mayun')]
# [('loca', 'shehzhen'), ('boss', 'mahuateng')]
# [('loca', 'shenzhen'), ('boss', 'renzhenfei')]
# [('chanpin', 'hangtianfeiji')]
At this time, we will find that there is no change in our file. At this time, we need:
8.write() function
def write(self, fp, space_around_delimiters=True):
"""Write an .ini-format representation of the configuration state.
with open(file_path,"w",encoding="gbk") as fw:
config.write(fw)
for x in config:
print(config.items(x))
# []
# [('loca', 'beijing'), ('boss', 'liuqiangdong')]
# [('loca', 'hangzhou'), ('boss', 'mayun')]
# [('loca', 'shehzhen'), ('boss', 'mahuateng')]
# [('loca', 'shenzhen'), ('boss', 'renzhenfei')]
# [('chanpin', 'hangtianfeiji')]
Essays on learning records are not perfect!
If there are any shortcomings, please correct me and discuss!