国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
python3操作INI格式文件
什么是INI格式

INI文件格式是某些平臺或軟件上的配置文件的非正式標準,以節(jié)(section)和鍵(key)構(gòu)成,常用于微軟Windows操作系統(tǒng)中。這種配置文件的文件擴展名多為INI,故名。


INI是英文“初始化”(initialization)的縮寫。正如該術(shù)語所表示的,INI文件被用來對操作系統(tǒng)或特定程序初始化或進行參數(shù)設(shè)置。


INI文件格式

節(jié)(section) 節(jié)用方括號括起來,單獨占一行,例如:


[section]


鍵(key)


鍵(key)又名屬性(property),單獨占一行用等號連接鍵名和鍵值,例如:


name=value


注釋(comment)


注釋使用英文分號(;)開頭,單獨占一行。在分號后面的文字,直到該行結(jié)尾都全部為注釋,例如:


; comment text


下面看一個用于MySQL數(shù)據(jù)庫鏈接參數(shù)配置實例


  1. [mysql]
  2. ;數(shù)據(jù)庫地址
  3. host=127.0.0.1
  4. ;數(shù)據(jù)庫鏈接端口
  5. port=3306
  6. ;數(shù)據(jù)庫用戶名
  7. user=root
  8. ;數(shù)據(jù)庫密碼
  9. password=123456
  10. ;默認鏈接的數(shù)據(jù)庫名
  11. db=autotest

Python ConfigParser類

在python里由標準模塊ConfigParser模塊提供的ConfigParser類實現(xiàn)對INI格式的文件進行讀寫,下面我們看看其主要的函數(shù),也是大家必須熟悉的。

  1. # 初始化
  2. cf = ConfigParser.ConfigParser() # 讀取ini文件,path為要讀取的ini文件的路徑
  3. cf.read(path)
  4. # 獲取所有sections。即將配置文件中所有“[ ]”讀取到列表中
  5. s = cf.sections()
  6. # 獲取指定section的options。
  7. # 即將配置文件某個section內(nèi)key 讀取到列表中
  8. o = cf.options("mysql")# 獲取指定section 的配置信息v = cf.items("msyql")# 按照類型讀取指定section 的option 信息# 同樣的還有g(shù)etfloat、getbooleandb_host = cf.get("mysql", "host")
  9. db_port = cf.getint("mysqldb", "port")
  10. db_user = cf.get("mysql", "user")
  11. db_pass = cf.get("mysql", "password")
  12. # 設(shè)置某個option 的值。(記得最后要保存)
  13. cf.set("mysql", "password", "654321")
  14. # 添加一個section。(同樣要保存)
  15. cf.add_section('oracle')
  16. cf.set('oracle', 'host', '127.0.0.1')
  17. cf.set('oracle', 'port', '5555')
  18. # 移除section 或者option (同樣要保存)
  19. cf.remove_option('oracle','port')
  20. cf.remove_section('oracle')

實例

下面我們對ConfigParser進行下簡單的封裝,形成我們自己的ini解析類,并演示如何使用。
  1. #-*- coding:utf-8 -*-
  2. from configparser import ConfigParser
  3. import os
  4. class LYMINIParser:
  5. def __init__(self, path):
  6. self.path = path
  7. self.ini = ConfigParser()
  8. self.ini.read(self.path)
  9. # 獲取sections列表
  10. def get_sections(self):
  11. if self.ini:
  12. return self.ini.sections()
  13. # 獲取指定的section的options列表
  14. def get_options_by_section(self, section):
  15. if self.ini:
  16. return self.ini.options(section)
  17. # 獲取指定section的配置信息列表
  18. def get_section_items(self, section):
  19. if self.ini:
  20. return self.ini.items(section)
  21. # 按類型讀取配置信息
  22. # 返回字符串類型
  23. def get_string(self, section, option):
  24. if self.ini:
  25. return self.ini.get(section, option)
  26. # 返回int類型
  27. def get_int(self, section, option):
  28. if self.ini:
  29. return self.ini.getint(section, option)
  30. # 返回float類型
  31. def get_float(self, section, option):
  32. if self.ini:
  33. return self.ini.getfloat(section, option)
  34. # 返回bool類型
  35. def get_boolean(self, section, option):
  36. if self.ini:
  37. return self.ini.getboolean(section, option)
  38. # 新增section
  39. def add_section(self, section):
  40. if self.ini:
  41. self.ini.add_section(section)
  42. self.ini.write(open(self.path, "w"))
  43. # 設(shè)置指定option值
  44. def set_option(self, section, option, value):
  45. if self.ini:
  46. self.ini.set(section, option, value)
  47. self.ini.write(open(self.path, "w"))
  48. # 刪除指定section
  49. def remove_section(self, section):
  50. if self.ini:
  51. self.ini.remove_section(section)
  52. self.ini.write(open(self.path, "w"))
  53. # 刪除指定option
  54. def remove_option(self, section, option):
  55. if self.ini:
  56. self.ini.remove_option(section, option)
  57. self.ini.write(open(self.path, "w"))
  58. if __name__ == "__main__":
  59. print("python ini標準庫解析實例")
  60. # 如果存在mysql.ini先刪除,方便下列代碼的運行
  61. if os.path.exists("mysql.ini"):
  62. os.remove("mysql.ini")
  63. # 我們先寫一些數(shù)據(jù)到mysql.ini中
  64. ini = LYMINIParser("mysql.ini")
  65. # 先加一個mysql section
  66. mysql_section = "mysql"
  67. ini.add_section(mysql_section)
  68. # 在mysql section下寫入一些配置信息
  69. ini.set_option(mysql_section, "host", "127.0.0.1")
  70. ini.set_option(mysql_section, "port", "3306")
  71. ini.set_option(mysql_section, "db", "autotesting")
  72. ini.set_option(mysql_section, "user", "root")
  73. ini.set_option(mysql_section, "password", "123456")
  74. # 再添加一個oracle section
  75. oracle_section = "oracle"
  76. ini.add_section(oracle_section)
  77. # oracle section下寫入一些配置信息
  78. ini.set_option(oracle_section, "host", "127.0.0.1")
  79. ini.set_option(oracle_section, "port", "1520")
  80. ini.set_option(oracle_section, "db", "auto_ui")
  81. ini.set_option(oracle_section, "user", "sa")
  82. ini.set_option(oracle_section, "password", "123456")
  83. # 獲取下所有的section,并在console輸出
  84. sections = ini.get_sections()
  85. print(sections)
  86. # 遍歷各個section下的options,并在console中輸出
  87. print("---" * 20) for sec in sections:
  88. print(sec, " 中的options為: ")
  89. options = ini.get_options_by_section(sec)
  90. print(options)
  91. print("---" * 20)
  92. # 獲取各個section下的配置信息
  93. for sec in sections:
  94. print(sec, " 中的配置信息為: ")
  95. items = ini.get_section_items(sec)
  96. print(items)
  97. print("---" * 20)
  98. # 獲取指定的option值這里演示讀取host和port
  99. host = ini.get_string("mysql", "host")
  100. port = ini.get_int("mysql", "port")
  101. print("類型: ", type(host), " ", type(port))
  102. print(host, " ", port)
  103. # 刪除mysql中的host配置
  104. ini.remove_option("mysql", "host")
  105. # 刪除oracle section
  106. ini.remove_section("oracle")
  107. # 修改mysql port的值為4000
  108. ini.set_option("mysql", "port", "4000")
  109. # 最終mysql.ini中的文件內(nèi)容如下
  110. #[mysql]
  111. #port = 4000
  112. #db = autotesting
  113. #user = root
  114. #password = 123456
小結(jié)

本文對ini格式的解析進行了較為完整的演示,大家可以根據(jù)需要對示例中的進一步封裝優(yōu)化掌握。

本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
每周一題——讀寫配置文件 Python討論區(qū)/精華帖
Python3 中 configparser 模塊解析配置的用法詳解
python開發(fā)項目,不得不了解的.cfg配置文件
python 操作ini 配置文件大全
ini配置文件操作方法
mysql5.6 服務啟動時報 1067錯誤的解決方法~
更多類似文章 >>
生活服務
分享 收藏 導長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服