Python settings file parser


















Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. What's the best practice using a settings file in Python? Asked 10 years, 11 months ago. Active 1 year, 1 month ago.

Viewed k times. Improve this question. I'd suggest using python-box , see this answer. I recommend giving trapdoor a try for turn-key configuration disclaimer: I'm the author of trapdoor. Add a comment. Active Oldest Votes. You can have a regular Python module, say config. Improve this answer. This is a pretty bad idea as if you want to allow low-privileged users to be able to change configuration files only, this way you're essentially allowing them to sneak in privileged code. I would highly recommend you use it.

PyYaml can be used to parse yaml. Json is already part of python from 2. Yaml is a superset of Json. It accomplishes this, in part, by having a long specification.

For Python to parse this, you will need to install a third-party module. However, the YAML parser expects a stream, so you need to convert the string into a stream. The specification is shorter, and it is already popular in some places for example, Rust's package manager, Cargo, uses it for package configuration. In order to parse TOML, you need to install a third-party package.

The most popular one is called, simply, toml. Choosing a configuration format is a subtle tradeoff. However, once you make the decision, Python can parse most of the popular formats using a handful of lines of code. Use Python to parse configuration files Opensource. Image by :. Get the highlights in your inbox every week. More Python Resources.

Improve this question. Andrei Ciobanu Andrei Ciobanu 12k 21 21 gold badges 80 80 silver badges bronze badges. Add a comment. Active Oldest Votes. You have to put whitespace on subsequent lines, and once parsed, the whitespace is removed. So all your multi-line strings get converted into one long string anyway. ConfigParser downcases all option names. There is no way to guarantee the order in which the options are written to file.

Improve this answer. Yes that was the problem. Thank you! Sign up or log in Sign up using Google.



0コメント

  • 1000 / 1000