Writing a dictionary to a text file?

前端 未结 10 786
挽巷
挽巷 2020-11-28 03:16

I have a dictionary and am trying to write it to a file.

exDict = {1:1, 2:2, 3:3}
with open(\'file.txt\', \'r\') as file:
    file.write(exDict)
10条回答
  •  借酒劲吻你
    2020-11-28 04:21

    You can do as follow :

    import json
    exDict = {1:1, 2:2, 3:3}
    file.write(json.dumps(exDict))
    

    https://developer.rhino3d.com/guides/rhinopython/python-xml-json/

提交回复
热议问题