Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- mysql
- VS Code
- 프로그래머스
- Docker
- 핑거스타일
- label
- 채보
- Linux
- nvidia-smi
- Numpy
- Visual Studio
- ubuntu
- C++
- pandas
- Selenium
- C
- C#
- 기타 연주
- SSH
- windows forms
- error
- pytorch
- Python
- 오류
- YOLO
- OpenCV
- pip
- paramiko
- 컨테이너
- JSON
Archives
- Today
- Total
기계는 거짓말하지 않는다
Python configparser config file 주석(comment) 유지 본문
Python의 configparser 모듈로 config file을 읽고 쓰면 config file의 주석은 유지되지 않는다.
주석을 유지해야 할 때, 가능하도록 간단히 구현한 예시이다.
import configparser
def read_config_with_comments(file_path):
"""config file과 config file의 line을 함께 read"""
with open(file_path, 'r') as file:
lines = file.readlines()
config = configparser.ConfigParser(allow_no_value=True)
config.read(file_path)
return config, lines
def write_config_with_comments(config, lines, output_file, space_around_delimiters=False):
"""config file 주석 유지"""
delim = " = " if space_around_delimiters else "="
pending = {
section: set(config[section].keys())
for section in config.sections()
}
current_section = None
with open(output_file, 'w') as f:
for idx, line in enumerate(lines):
stripped = line.strip()
# (1) Preserve comments and blank lines as is
if not stripped or stripped.startswith("#"):
f.write(line)
continue
if stripped.startswith("[") and stripped.endswith("]"):
# If we just finished a section, write any remaining keys
if current_section:
for key in list(pending[current_section]):
f.write(f"{key}{delim}{config[current_section][key]}\n")
pending[current_section].clear()
current_section = stripped[1:-1]
f.write(line)
continue
if "=" in line and current_section:
key = line.split("=", 1)[0].strip()
if key in config[current_section]:
# Write the updated value
f.write(f"{key}{delim}{config[current_section][key]}\n")
pending[current_section].discard(key)
else:
# Write the original line if the key is not updated
f.write(line)
else:
f.write(line)
is_last = (idx == len(lines) - 1)
next_is_section = False
if not is_last:
nl = lines[idx+1].strip()
next_is_section = nl.startswith("[") and nl.endswith("]")
if current_section and (is_last or next_is_section):
for key in list(pending[current_section]):
f.write(f"{key}{delim}{config[current_section][key]}\n")
pending[current_section].clear()
for section, keys in pending.items():
if keys:
f.write(f"\n[{section}]\n")
for key in keys:
f.write(f"{key}{delim}{config[section][key]}\n")
'Python' 카테고리의 다른 글
Python faulthandler 모듈 Segmentation Fault 진단 (0) | 2025.01.25 |
---|---|
Python colorsys 모듈을 이용한 일정한 분포의 색상 생성 함수 (0) | 2025.01.21 |
Python open 함수 파일 모드(mode) (3) | 2024.09.03 |
Python paramiko 프롬프트 상호작용 invoke_shell (0) | 2024.08.20 |
Python logging 모듈 logger 설정(Settings) (0) | 2024.07.10 |
Comments