-1

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

What I have:

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

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

key1=sentence1
key2=sentence2
MattDMo
  • 100,794
  • 21
  • 241
  • 231
pikachu
  • 690
  • 1
  • 6
  • 17

2 Answers2

2

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)
lvjonok
  • 109
  • 6
1

Try this:

my_dictionary={"key1":"sentence1",  "key2":"sentence2"}
with open("filename.properties", "w") as file:
    list_items=my_dictionary.items()
    for item in list_items:
        file.write(f"{item[0]}={item[1]}\n")
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
tetris programming
  • 809
  • 1
  • 2
  • 13