zoukankan      html  css  js  c++  java
  • python模块之configparser

    https://docs.python.org/3/library/configparser.html

    configparser用于处理特定格式的文件,其本质上是利用open来操作文件。

    1、获取所有节点

    import configparser
     
    config = configparser.ConfigParser()
    config.read('xxxooo', encoding='utf-8')
    ret = config.sections()
    print(ret)

    2、获取指定节点下所有的键值对

    import configparser
     
    config = configparser.ConfigParser()
    config.read('xxxooo', encoding='utf-8')
    ret = config.items('section1')
    print(ret)

    3、获取指定节点下所有的建

    import configparser
     
    config = configparser.ConfigParser()
    config.read('xxxooo', encoding='utf-8')
    ret = config.options('section1')
    print(ret)

    4、获取指定节点下指定key的值

    复制代码
    import configparser
     
    config = configparser.ConfigParser()
    config.read('xxxooo', encoding='utf-8')
     
     
    v = config.get('section1', 'k1')
    # v = config.getint('section1', 'k1')
    # v = config.getfloat('section1', 'k1')
    # v = config.getboolean('section1', 'k1')
     
    print(v)
    复制代码

    5、检查、删除、添加节点

    复制代码
    import configparser
     
    config = configparser.ConfigParser()
    config.read('xxxooo', encoding='utf-8')
     
     
    # 检查
    has_sec = config.has_section('section1')
    print(has_sec)
     
    # 添加节点
    config.add_section("SEC_1")
    config.write(open('xxxooo', 'w'))
     
    # 删除节点
    config.remove_section("SEC_1")
    config.write(open('xxxooo', 'w'))
    复制代码

    6、检查、删除、设置指定组内的键值对

    复制代码
    import configparser
     
    config = configparser.ConfigParser()
    config.read('xxxooo', encoding='utf-8')
     
    # 检查
    has_opt = config.has_option('section1', 'k1')
    print(has_opt)
     
    # 删除
    config.remove_option('section1', 'k1')
    config.write(open('xxxooo', 'w'))
     
    # 设置
    config.set('section1', 'k10', "123")
    config.write(open('xxxooo', 'w'))
    复制代码
    import sys,os
    import configparser
    
    config = configparser.ConfigParser()
    config.read("xxxooo",encoding='utf-8')
    sections = config.sections()
    print(sections)
    #baseconf = print(config.items("baseconf"))
    a = config.options("baseconf")
    print(a)
    v = config.get('baseconf','port')
    print(v)
    hs_sec = config.has_section('baseconf')
    print(hs_sec)
    #config.add_section("SEC_2")
    #config.write(open('xxxooo', 'w'))
    
    config.set('SEC_2', 'k10', "123")
    config.write(open('xxxooo', 'w'))
    

      

  • 相关阅读:
    SGU 495 Kids and Prizes 概率DP 或 数学推理
    poj 2799 IP Networks 模拟 位运算
    uva 202 Repeating Decimals 模拟
    poj 3158 Kickdown 字符串匹配?
    uva 1595 Symmetry 暴力
    uva 201 Squares 暴力
    uva 1594 Ducci Sequence 哈希
    uva 1368 DNA Consensus String 字符串
    数字、字符串、列表的常用操作
    if条件判断 流程控制
  • 原文地址:https://www.cnblogs.com/brady-wang/p/8390872.html
Copyright © 2011-2022 走看看