2021年3月26日星期五

Question about usage of Python json.dump()

I'm practicing how to make a program using Python and suddenly I got an issue.
When I save a Dictionary type date into JSON, I use standard JSON library from Python and I can save data with the code below.

data = {      'a': 'aa'      , 'b': 'bb'  }    with open('data.json', 'w', encoding='utf-8') as f:      json.dump(data, f)  

But, I can't save data into file with this.

data = {      'a': 'aa'      , 'b': 'bb'  }    f = open('data.json', 'w', encoding='utf-8')  json.dump(data, f)  f.close()  

I want to know the reason why upper one is working but lower one is not working.
Please explain it for me.
Thank you in advance.

https://stackoverflow.com/questions/66827348/question-about-usage-of-python-json-dump March 27, 2021 at 09:59AM

没有评论:

发表评论