如何使用 jsonplus dump/write 到一个文件?
How to dump/write to a file with jsonplus?
jsonplus 似乎没有 .dump(),只有 .dumps() 函数,那么您将如何使用 jsonplus 写入和读取命名元组列表?
大意为:
import jsonplus as json
from collections import namedtuple
Person = namedtuple('Person', 'name age')
l = [Person('bob', '28'),
Person('joe', '29'),
Person('tom', '30')]
with open('test.json', 'w') as outfile:
json.dump(l, outfile, indent = 4)
with open('test.json', "r") as read_file:
read_data = json.load(read_file)
您应该将 dumps
的字符串结果写入文件
with open('test.json', 'w') as outfile:
outfile.write(json.dumps(l))
with open('test.json', 'r') as read_file:
read_data = json.loads(read_file.read())
jsonplus 似乎没有 .dump(),只有 .dumps() 函数,那么您将如何使用 jsonplus 写入和读取命名元组列表?
大意为:
import jsonplus as json
from collections import namedtuple
Person = namedtuple('Person', 'name age')
l = [Person('bob', '28'),
Person('joe', '29'),
Person('tom', '30')]
with open('test.json', 'w') as outfile:
json.dump(l, outfile, indent = 4)
with open('test.json', "r") as read_file:
read_data = json.load(read_file)
您应该将 dumps
with open('test.json', 'w') as outfile:
outfile.write(json.dumps(l))
with open('test.json', 'r') as read_file:
read_data = json.loads(read_file.read())