Python Glossary Русский

all Python 3configparserterm

configparser.RawConfigParser

parsers

The legacy base variant of ConfigParser without interpolation: a % sign in values is read as is, with no substitutions.

Syntax

parser = configparser.RawConfigParser(defaults=None, dict_type=dict, allow_no_value=False)

People reach for it to keep values like '50%' or passwords containing %, but it has a trap: set() does not check types, so the integer 5 stays an int instead of the string you would get from a file. The docs recommend ConfigParser(interpolation=None) instead: no substitutions either, but the strings-only check is kept.

Examples

import configparser
raw = configparser.RawConfigParser()
raw.read_string("[db]\nrate = 50%\n")
print(raw.get("db", "rate"))  # → 50%
raw.set("db", "port", 5432)  # ConfigParser ответил бы TypeError
print(type(raw.get("db", "port")).__name__)  # → int — тип не тот, что у прочитанного
import configparser
cfg = configparser.ConfigParser(interpolation=None)
cfg.read_string("[db]\nrate = 50%\n")
print(cfg.get("db", "rate"))  # → 50%
try:
    cfg.set("db", "port", 5432)
except TypeError as e:
    print(e)  # → option values must be strings

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy