-4

in python I have:

dict = {}
dict['test'] = 'test'

when I print I get:

{'test':'test'}

How can I make it like this:

{"test":"test"}

Please Note, replace won't work as test may be test't...

I tried:

dict = {}
dict["test"] = "test"
David
  • 9
  • 7
  • 2
    Python's standard library has a module specifically for working with JSON, please do basic research before asking on SO. – jonrsharpe Jan 12 '22 at 10:00
  • You can use the `json` module as shown [here](https://stackoverflow.com/a/32824345/11078962). – JMad Jan 12 '22 at 10:01
  • 3
    Googling "python json" would have given you the answer after a few seconds. Posting this question was more effort than doing the research. – luk2302 Jan 12 '22 at 10:05
  • Does this answer your question? [Python - dump dict as a json string](https://stackoverflow.com/questions/16668511/python-dump-dict-as-a-json-string) – Taylor D. Edmiston Jan 15 '22 at 19:36

1 Answers1

1

You can use json.dumps() For example, if you use print json.dumps(dict) you should get the desired output.

Additionally, as suggested in a different related question, you may construct your own version of a dict with special printing:

How to create a Python dictionary with double quotes as default quote format?

Belf
  • 31
  • 1
  • 5