当前位置: 移动技术网 > IT编程>脚本编程>Python > Python ini文件常用操作方法解析

Python ini文件常用操作方法解析

2020年05月11日  | 移动技术网IT编程  | 我要评论

落日海港,半球电磁炉电路图,76复古

一、config.ini 配置文件

[database]
host = 192.1.1.1
username = root
password = root
port = 3306
database = jforum

[url]
#ip,端口
ip =127.0.0.1
port= 8089

二、操作ini常用方法

--read():读取配置文件
--sections():读取配置文件中所有的section(如上配置文件:database,url)
--options(section):读取该section下所有的option(可以理解成读取该组下的所有key,如options("url"),值['ip', 'port'])
--items(section):读取该section下的所有key-vaule,并以键值对形式输出(如:sectioitems("url"),值:[('ip', '127.0.0.1'), ('port', '8089')])
--get(section, option):读取指定section下面的option的值(可以理解成,读取具体某个section下面指定key的值,如config.get('url','ip')),值:127.0.0.1)
--add_section(section):添加一个section,参数为section的名称
--set(section, option, value):在section下面添加一条数据(key=value)
--add与set需调用write(open(configpath, "a"))才可以写入ini文件 #参数a表示最近,w重写
--remove_seciton(seciton) 删除整个seciton
--config.remove_option(seciton,key) ,删除seciton的某个key值

三、源码举例

#!/usr/bin/python3
# encoding:utf-8
'''
created on 2020-04-19 23:19
@author: administrator
'''
import configparser
import os
from turtle import readconfig

#获取文件绝对路径 d:\common\
prodir = os.getcwd()
#拼接文件路径 d:\common\config.ini
configpath = os.path.join(prodir, "config.ini")

#创建管理对象
config = configparser.configparser()
#读取配置类
class readconfig():
  #读取ini文件
  config.read(configpath, encoding="utf-8")
  #获取所有的section
  @staticmethod
  def get_sections():
    return config.sections()
  @staticmethod
  def get_items(section):
    return config.items(section)
  @staticmethod
  def get_options(section):
    return config.options(section)
  @staticmethod
  def get_vaule(section,name):
    value = config.get(section, name)
    return value
  @staticmethod
  def add_section():
    config.add_section('http')
  @staticmethod
  def set_section(section, option, value):
    config.set(section, option, value)
  @staticmethod
  def remove_seciton(seciton):
    config.remove_section(seciton)
  @staticmethod
  def remove_seciton_value(seciton,key):
    config.remove_option(seciton,key)   
if __name__=='__main__':
  print('-----1.打印所有section')
  print(readconfig.get_sections())
  print('-----2.打印section=url的所有key-value值')
  print(readconfig.get_items("url"))
  print('-----3.打印section=url的所有key值')
  print(readconfig.get_options("url"))
  print('-----4.打印section=url,key=ip的value值')
  print(readconfig.get_vaule('url','ip'))
  print('-----5.新增之后打印所有section,注意有一个新增值http')
  readconfig.add_section()
  print(readconfig.get_sections())
  print('-----6.新增section=http,key=port,value=443,查看值,443为新增的值')
  readconfig.set_section('http', 'port', '443')
  print(readconfig.get_vaule('http','port'))
  #上面的新增并不会真的真正写入,需加这个才能正在写入ini文件,如果参数为"w"则表示删除文件重新写入,"a"为追加模式写入
  #config.write(open(configpath, "a")) 
  print('-----7.删除sections=url,打印所有sections,注意url已被删除')
  readconfig.remove_seciton("url")
  print(readconfig.get_sections())
  print('-----8.删除sections=database,key=host,打印所有key值,注意host已被删除')
  readconfig.remove_seciton_value('database','host')
  print(readconfig.get_options('database'))

运行结果

-----1.打印所有section
['database', 'url']
-----2.打印section=url的所有key-value值
[('ip', '127.0.0.1'), ('port', '8089')]
-----3.打印section=url的所有key值
['ip', 'port']
-----4.打印section=url,key=ip的value值
127.0.0.1
-----5.新增之后打印所有section,注意有一个新增值http
['database', 'url', 'http']
-----6.新增section=http,key=port,value=443,查看值,443为新增的值
443
-----7.删除sections=url,打印所有sections,注意url已被删除
['database', 'http']
-----8.删除sections=database,key=host,打印所有key值,注意host已被删除
['username', 'password', 'port', 'database']

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网