Keep key names case in Python standard ConfigParser

You can read and write INI files with the standard Python library configparser. https://docs.python.org/ja/3/library/configparser.html

An INI file is a file format that is sometimes used for configuration files, etc., which consists of sections, keys, and values. https://ja.wikipedia.org/wiki/INI%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB

By default, configparser is case insensitive for keys.

import configparser


ini_filename = "sample.ini"
"""Contents of INI file
[General]
Name = hogehoge
"""

parser = configparser.ConfigParser()
parser.read(ini_filename)

"""uppercase letter/Lowercase/Can be read regardless of mixing"""
assert parser["General"]["Name"] == "hogehoge", "Mixed"
assert parser["General"]["name"] == "hogehoge", "Lowercase"
assert parser["General"]["NAME"] == "hogehoge", "uppercase letter"

This works for both reading and writing INI files.

The sidecar file used in the software called RawTherapee has the contents like an INI file, I wanted to edit this all at once, but RawTherapee's sidecar file is case sensitive for key names. Therefore, if you edit / save as it is with the ConfigParser class, the key name will be all lowercase. There was a problem that RawTherapee could not be read properly.

This can be solved by setting the appropriate function in .optionxform of the ConfigParser instance. For the key name when reading and writing, the result converted by the ʻoptionxform` function is used.

Therefore, if you set ʻoptionxform` as follows, you can save the INI file while preserving the key name.

parser = configparser.ConfigParser()
parser.optionxform = str
parser.read(ini_filename)

parser["General"]["Name"] = "test"

with open(ini_filename, "w") as fp:
    parser.write(fp)

Recommended Posts

Keep key names case in Python standard ConfigParser
Key input in Python
Key input in Python
Transposed matrix in Python standard
Make standard output non-blocking in Python
Foreign Key in Python SQLite [Note]
Create a standard normal distribution graph in Python
Copy data between Google Keep accounts in Python
How to output "Ketsumaimo" as standard output in Python
Standard .py file used in Python trials (template)-2020
Split camel case string word by word in Python
Flatten an irregular 2D standard list in Python
Quadtree in Python --2
Python in optimization
CURL in python
My ConfigParser (Python)
Metaprogramming in Python
Python 3.3 in Anaconda
SendKeys in Python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
My ConfigParser (Python)
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Things to keep in mind when developing crawlers in Python
Calculation of standard deviation and correlation coefficient in Python
Key input that does not wait for key input in Python
Get standard output in real time with Python subprocess
Output log in JSON format with Python standard logging
Calculate mean, median, mode, variance, standard deviation in Python
Can be used in competition pros! Python standard library
[Work efficiency] How to change file names in Python