首页 > Web开发 > 详细

json 读写数据

时间:2019-10-23 14:37:32      阅读:110      评论:0      收藏:0      [点我收藏+]

import json

class OptertionJson:

def __init__(self, file_path=None):
if file_path == None:
self.file_path = ‘./test.json‘
else:
self.file_path = file_path
self.data = self.get_file()


‘‘‘
读取json配置文件
‘‘‘
def get_file(self):
with open(self.file_path) as fp:
json_data = fp.read()
return json.loads(json_data)

‘‘‘
根据关键字获取数据
‘‘‘
def get_value(self, key):
value = self.data[key]
return value

‘‘‘
写入数据
‘‘‘
def set_write(self, data):
with open(‘./test.json‘,‘w‘) as fp:
fp.write(json.dumps(data))


if __name__ == ‘__main__‘:
oj = OptertionJson()
s = oj.get_value(‘head‘)
print(s)
print(type(oj.data))
# oj.set_write("asdfasf")

json 读写数据

原文:https://www.cnblogs.com/xinyueqingfeng/p/11725863.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!