如何使用jsonplus转储/写入文件?

问题描述 投票:0回答:1

jsonplus似乎没有.dump(),只有一个.dumps()函数,所以您将如何使用jsonplus编写和读取例如namedtuple的列表?

具有以下作用:

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)
python json file-io
1个回答
0
投票
with open('test.json', 'w') as outfile:
     outfile.write(json.dump(l))

with open('test.json', 'r') as read_file:
    read_data = json.loads(read_file.read())
© www.soinside.com 2019 - 2024. All rights reserved.