改变字典内的value

import re,os
limit = "8000"
username = "liuhuihuang"

with open("users_dict","r",encoding="utf8") as price_file,open("new_users_dict","w+",encoding="utf8") as write_file:
    list = json.loads(price_file.read())

    if username in list.keys():
        list[username]["limit"] = limit
        write_file.write(json.dumps(list))
os.rename("users_dict","users_dict.bak")
os.rename("new_users_dict","users_dict")
os.remove("users_dict.bak")
users_dict内容如下:
{"liuhuihuang": {"password": "123456", "username": "liuhuihuang", "limit": "10000", "locked": 0}, "alex": {"password": "123456", "username": "alex", "limit": "20000", "locked": 0}}

该段代码的作用就是把用户名为liuhuihuang的limit值从10000改成8000,我这算是比较笨的方法了,蛋疼。

posted @ 2016-11-10 22:55  梦轻尘  阅读(629)  评论(0编辑  收藏  举报