Python全栈开发:configparser模块
生活随笔
收集整理的這篇文章主要介紹了
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"))
轉載于:https://www.cnblogs.com/nixingguo/p/6497285.html
總結
以上是生活随笔為你收集整理的Python全栈开发:configparser模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux下使Shell命令脱离终端运行
- 下一篇: [TPYBoard - Micropyt