INI格式
INI格式是最常见的配置文件格式之一,Python的标准库`configparser`可以用来读取和写入INI格式的配置文件。
```python
创建配置文件
config = ConfigParser()
config['MOTOR'] = {
'comnum': '3',
'baud': '19200',
'm1slowstep': '10',
'm1faststep': '100',
'm1origin': '5',
'm2slowstep': '10',
'm2faststep': '50',
'm2origin': '5'
}
config['CoarseAdjust'] = {
'standardx': '0.000000',
'standardy': '0.000000',
'xperangle': '500',
'yperangle': '160',
'xmotor': '1',
'xmotororien': '-1',
'ymotor': '2',
'ymotororien': '1',
'triggermode': '1',
'triggertimeout': '1',
'autoadjust': '1'
}
config['FineAdjust'] = {
'countdown': '10',
'datfilepath': 'E:\\Mcs05\\DatTemp\\',
'xfinestep': '10',
'yfinestep': '10',
'mcsfilepath': 'E:\\Mcs05\\WHTest\\',
'filetype': 'Mcs',
'nastartaltitude': '80',
'naendaltitude': '111',
'rayleighstartaltitude': '20',
'rayleighendaltitude': '60'
}
保存配置文件
with open('config.ini', 'w') as configfile:
config.write(configfile)
JSON格式
JSON格式也是一种常见的配置文件格式,Python内置了`json`库来处理JSON文件。
```python
import json
创建配置文件
config = {
"MOTOR": {
"comnum": "3",
"baud": "19200",
"m1slowstep": "10",
"m1faststep": "100",
"m1origin": "5",
"m2slowstep": "10",
"m2faststep": "50",
"m2origin": "5"
},
"CoarseAdjust": {
"standardx": "0.000000",
"standardy": "0.000000",
"xperangle": "500",
"yperangle": "160",
"xmotor": "1",
"xmotororien": "-1",
"ymotor": "2",
"ymotororien": "1",
"triggermode": "1",
"triggertimeout": "1",
"autoadjust": "1"
},
"FineAdjust": {
"countdown": "10",
"datfilepath": "E:\\Mcs05\\DatTemp\\",
"xfinestep": "10",
"yfinestep": "10",
"mcsfilepath": "E:\\Mcs05\\WHTest\\",
"filetype": "Mcs",
"nastartaltitude": "80",
"naendaltitude": "111",
"rayleighstartaltitude": "20",
"rayleighendaltitude": "60"
}
}
保存配置文件
with open('config.json', 'w') as configfile:
json.dump(config, configfile)
YAML格式
YAML格式也是一种流行的配置文件格式,可以使用`PyYAML`库来处理。