python处理json格式的文件

来源:互联网 发布:管家婆支持远程网络吗 编辑:程序博客网 时间:2024/06/05 04:37
import json,timeinfos = {"_id":"description","name":"python","filename":"中文","os":["abcd","hello","www"]}infos["time"] = time.time()#动态修改json文件内容#生成json文件def json_file(infos):    with open("./static/desc.desc","w") as jsonf:        <span style="font-family: Arial, Helvetica, sans-serif;">jsonf</span><span style="font-family: Arial, Helvetica, sans-serif;">.write(json.dumps(infos))</span>json_file(infos)#读取json文件的内容file_info = json.load(file("./static/desc.desc"))print file_info,type(file_info)filename = file_info["filename"]print filenameinfos = json.dumps(file_info,sort_keys=True,indent=4)print infos,type(infos)


输出内容:

{u'filename': u'\u4e2d\u6587', u'_id': u'description', u'os': [u'abcd', u'hello', u'www'], u'name': u'python', u'time': 1463415343.072} <type 'dict'>
中文
{
    "_id": "description", 
    "filename": "\u4e2d\u6587", 
    "name": "python", 
    "os": [
        "abcd", 
        "hello", 
        "www"
    ], 
    "time": 1463415343.072
} <type 'str'>

1 0
原创粉丝点击