到目前为止,这是我的代码,我还是编程的初学者,所以请帮助我,我非常迷失
import json
try:
file = open("information.txt", "rt")
except:
print("no file was found")
dict = {
"a":1,
"b":2,
"c":3
}
open('information.txt', 'w')
json.dump(dict, file)
open('information.txt', 'r')
file2 = json.load(file)
print(file2.read())
阅读:
import json
from pathlib import Path
try:
data = json.loads(Path('information.txt').read_text())
except FileNotFoundError:
print("No file was found!")
撰写:
import json
from pathlib import Path
data = {
"a": 1,
"b": 2,
"c": 3
}
data = Path('information.txt').write_text(json.dumps(data, indent=4))
此外,请不要使用dict
作为变量名,因为它是Python中的保留关键字。