회원 로그인
|
Config Parser
http://nefaria.com/2012/08/simple-configuration-file-parser-python
#cfg
#Example configuration file #Foo Bar-er v1.0 foo_dir="/var/lib/foo" bar_dir="/var/lib/bar" foo_all_the_bars="1" bar_all_the_foos = "yes, do it"#code
# SimpleConfigParser # Inspired by: # http://www.decalage.info/fr/python/configparser class SimpleConfigParser(): def __init__(self, comment_char = '#', option_char = '=', allow_duplicates = False, strip_quotes = True): self.comment_char = comment_char self.option_char = option_char self.allow_duplicates = allow_duplicates self.strip_quotes = True def parse_config(self, filename): self.options = {} config_file = open(filename) for line in config_file: if self.comment_char in line: line, comment = line.split(self.comment_char, 1) if self.option_char in line: option, value = line.split(self.option_char, 1) option = option.strip() value = value.strip() value = value.strip('"'') if self.allow_duplicates: if option in self.options: if not type(self.options[option]) == list: old_value = self.options[option] self.options[option] = [value] + [old_value] else: self.options[option] += [value] else: self.options[option] = value else: self.options[option] = value config_file.close() return self.options#
>>> from simpleconfig import SimpleConfigParser >>> scp = SimpleConfigParser() >>> scp.parse_config('example.cfg') {'bar_dir': '/var/lib/bar', 'foo_all_the_bars': '1', 'foo_dir': '/var/lib/foo', 'bar_all_the_foos': 'yes, do it'}#
>>> from simpleconfig import SimpleConfigParser >>> scp = SimpleConfigParser(allow_duplicates = True) >>> nagios_config = scp.parse_config('/etc/nagios3/nagios.cfg') >>> nagios_config['cfg_dir'] ['/etc/nagios3/conf.d', '/etc/nagios-plugins/config']
댓글 0개
| 엮인글 0개
285개(1/15페이지)
번호 | 제목 | 글쓴이 | 조회 | 날짜 |
---|---|---|---|---|
285 | [Python] 동적 import - 모듈을 변수로 받아오기 | 푸딩뱃살 | 445 | 2022.10.27 10:45 |
284 | [Python] 파이썬 3.7.7과 3.9.7의 os.path.expanduser() 차이 | 푸딩뱃살 | 479 | 2022.08.18 12:22 |
283 | [Python] error: Microsoft Visual C++ 9.0 is required. | 푸딩뱃살 | 721 | 2022.08.03 13:35 |
282 | [Python] pyscript | 푸딩뱃살 | 486 | 2022.06.09 11:21 |
281 | [Python] float is / float not is | 푸딩뱃살 | 617 | 2022.03.02 15:03 |
280 | [Python] 이터널 문자열 f | 푸딩뱃살 | 870 | 2022.01.27 16:35 |
279 | [Python] is와 ==의 차이 | 푸딩뱃살 | 502 | 2021.11.25 15:54 |
278 |
[Python] Error: ImportError: file |
푸딩뱃살 | 971 | 2021.11.16 11:24 |
277 | [Python] 파이썬 디컴파일 - uncompyle6 | 푸딩뱃살 | 806 | 2021.11.10 14:46 |
276 | [Python] 파이썬 확장자 설명 | 푸딩뱃살 | 572 | 2021.11.03 14:38 |
275 | [참고] 웹 fbx 뷰어 | 푸딩뱃살 | 488 | 2021.10.19 15:46 |
274 | [Python] enumerate() | 푸딩뱃살 | 523 | 2021.10.13 14:44 |
273 | [Python] 아나콘다에서 가상 환경 | 푸딩뱃살 | 763 | 2020.11.21 00:26 |
272 | [Python] pip로 설치 때 퍼미션 에러 | 푸딩뱃살 | 1301 | 2020.06.06 17:13 |
271 | [Python] OpenCV 10-3. 이미지 Thresholding - Otsu's Binarizatio | 푸딩뱃살 | 689 | 2020.06.05 14:01 |
270 | [Python] OpenCV 10-2. 이미지 Thresholding - Adaptive Threshold | 푸딩뱃살 | 713 | 2020.06.05 13:58 |
269 | [Python] OpenCV 10-1. 이미지 Thresholding | 푸딩뱃살 | 592 | 2020.06.05 13:56 |
268 | [Python] OpenCV 9-2. 색 추적 | 푸딩뱃살 | 809 | 2020.06.02 23:29 |
267 | [Python] OpenCV 9-1. 색공간 바꾸기 | 푸딩뱃살 | 655 | 2020.06.02 23:27 |
266 | [Python] OpenCV 8-3. 이미지 비트 연산 | 푸딩뱃살 | 546 | 2020.06.02 23:21 |