Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to export dictionary as a .property file in python

How can a dictionary in python be exported to .properties file type in python?

What I have:

dict = {"key1":"sentence1", "key2":"sentence2"}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The object dict should be saved as .properties file in python, the output should look like this:

key1=sentence1
key2=sentence2

>Solution :

You could use python built-in library configparser

import configparser

dict = {"key1":"sentence1",  "key2":"sentence2"}

config = configparser.ConfigParser()

# some your default section
section_name = "default"
config.add_section(section_name)

for (k, v) in dict.items():
    config.set(section_name, k, v)

with open("config.properties", "w") as f:
    config.write(f)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading