• Python全栈开发:configparser模块


    #!/usr/bin/env python
    # -*- coding;utf-8 -*-
    import configparser
    
    # 创建对象
    conn = configparser.ConfigParser()
    
    # conn对象具有read功能,打开文件读取文件,放入内存,要保存必须重写
    conn.read("ini", encoding="utf-8")
    
    # conn对象的sections作用:在内存中寻找所有的[???]
    result = conn.sections()
    print(result)
    
    # 从指定的[???]中寻找所有的key
    result = conn.options("section2")
    print(result)
    
    # 获取指定节点下key的值
    result = conn.get("section1", "k1")
    result = conn.getint("section1", "k1")
    result = conn.getfloat("section1", "k1")
    result = conn.getboolean("section1", "k1")
    print(result)
    
    # 判断是否存在某个节点
    result = conn.has_section("section1")
    print(result)
    
    # 判断某个节点下是否存在key
    result = conn.has_option("section1", "k1")
    print(result)
    
    # 移除某个节点的键值对
    result = conn.remove_option("section1", "k1")
    conn.write(open("文件名", "w"))  # 参数是一个可写的对象
    
    # 设置某个节点的键值对,涉及到中文要明确编码,否则出现乱码
    result = conn.set('a', "k5", "你好吗")
    conn.write(open("好看吗", "w", encoding="utf-8"))
    

      

  • 相关阅读:
    次小生成树
    [bzoj5329] P4606 [SDOI2018]战略游戏
    CF487E Tourists
    P3225 [HNOI2012]矿场搭建
    CF #636 (Div. 3) 对应题号CF1343
    P3469 [POI2008]BLO-Blockade
    大假期集训模拟赛12
    大假期集训模拟赛11
    大假期集训模拟赛10
    小奇画画——BFS
  • 原文地址:https://www.cnblogs.com/nixingguo/p/6497285.html
Copyright © 2020-2023  润新知