You are currently viewing ini 格式

ini 格式

ini 檔案格式處理

Python 也可以很容易的處理 ini 檔案格式,只不過需要安裝一個 module

參考網址:https://docs.python.org/3/library/configparser.html

config 設定文檔處理

conf 常見格式,mysql.cnf 格式範例:

[mysqld]
performance-schema=0
local-infile=0
innodb_file_per_table=1
innodb_buffer_pool_size=1G
#default-storage-engine=MyISAM
#log-bin=mysql-bin
#performance_schema=on
sql_mode=NO_AUTO_CREATE_USER
max_connections=600
key_buffer_size=128M
myisam_sort_buffer_size=64M
join_buffer_size=16M
sort_buffer_size=16M
table_open_cache=2000
thread_cache_size=128
wait_timeout=60
interactive_timeout=60
connect_timeout=10
max_allowed_packet=268435456
max_connect_errors=1000
max_user_connections=40
query_cache_size=96M
query_cache_limit=4M
#datadir=/var/lib/mysql
#socket=/var/lib/mysql/mysql.sock
#skip-name-resolve
open_files_limit=65535
tmp_table_size=256M
max_heap_table_size=256M
tmpdir = /dev/shm/
default-storage-engine=MyISAM
[mysqld_safe]
#err-log=/var/log/mysqld.log
#pid-file=/var/run/mysqld/mysqld.pid
open_files_limit=8192
[mysqldump]
quick
max_allowed_packet=16M
[myisamchk]
key_buffer=64M
sort_buffer=64M
read_buffer=16M
write_buffer=16M

設定檔生成

import configparser

config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '45',
                    'Compression': 'yes',
                    'CompressionLevel': '9'}

config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022' # mutates the parser
topsecret['ForwardX11'] = 'no' # same here
config['DEFAULT']['ForwardX11'] = 'yes'

with open('example.ini', 'w') as configfile:
    config.write(configfile)

讀取設定檔

>>> import configparser
>>> config = configparser.ConfigParser()
>>> config.sections()
[]
>>> config.read('example.ini')
['example.ini']
>>> config.sections()
['bitbucket.org', 'topsecret.server.com']
>>> 'bitbucket.org' in config
True
>>> 'bytebong.com' in config
False
>>> config['bitbucket.org']['User']
'hg'
>>> config['DEFAULT']['Compression']
'yes'
>>> topsecret = config['topsecret.server.com']
>>> topsecret['ForwardX11']
'no'
>>> topsecret['Port']
'50022'
>>> for key in config['bitbucket.org']: print(key)
...
user
compressionlevel
serveraliveinterval
compression
forwardx11
>>> config['bitbucket.org']['ForwardX11']
'yes'

configparser 增刪改查語法

[section1]
k1 = v1
k2:v2

[section2]
k1 = v1

import ConfigParser

config = ConfigParser.ConfigParser()
config.read('i.cfg')

# ########## 讀 ##########
secs = config.sections()
print secs
options = config.options('group2')
print options

item_list = config.items('group2')
print item_list

val = config.get('group1','key')
val = config.getint('group1','key')

# ########## 改寫 ##########
sec = config.remove_section('group1')
config.write(open('i.cfg', "w"))

sec = config.has_section('wupeiqi')
sec = config.add_section('wupeiqi')
config.write(open('i.cfg', "w"))

config.set('group2','k1',11111)
config.write(open('i.cfg', "w"))

config.remove_option('group2','age')
config.write(open('i.cfg', "w"))

Beck Yeh

熱愛學習於 Linux 與 程式設計 在網站中分享各式各樣學習到的新知識

發佈留言

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料