Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:将多个json对象写入一个文件;稍后将通过json.load打开并加载_Python_Json - Fatal编程技术网

Python:将多个json对象写入一个文件;稍后将通过json.load打开并加载

Python:将多个json对象写入一个文件;稍后将通过json.load打开并加载,python,json,Python,Json,我已经从数据流构造了一个json对象,其中每个用户都有一个json对象 { "entities": { "description": { "urls": [] } }, "utc_offset": -10800, "id" "name": "Tom", "hit_count": 7931, "private": false, "active_last_month": false

我已经从数据流构造了一个json对象,其中每个用户都有一个json对象

{
    "entities": {
        "description": {
            "urls": []
        }
    },
    "utc_offset": -10800,
    "id"
    "name": "Tom",
    "hit_count": 7931,
    "private": false,
    "active_last_month": false,
    "location": "",
    "contacted": false,
    "lang": "en",
}
目标:我想构造一个json文件,其中每个json对象都成为一个缩进文件中的一行。在读回JSON文件时,可以使用with open进行读取:

例如:下面的文件

[
{
    "entities": {
        "description": {
            "urls": []
        }
    },
    "utc_offset": -10800,
    "id"
    "name": "Tom",
    "hit_count": 7931,
    "private": false,
    "active_last_month": false,
    "location": "",
    "contacted": false,
    "lang": "en",
}
,
{
    "entities": {
        "description": {
            "urls": []
        }
    },
    "utc_offset": -10500,
    "id"
    "name": "Mary",
    "hit_count": 554,
    "private": false,
    "active_last_month": false,
    "location": "",
    "contacted": false,
    "lang": "en",
}
]
上述文件可通过以下方式轻松读取:

with open(pathToFile) as json_file:
     json_data = json.load(json_file)
     for key in json_data:
         print key["id"]
但现在我写的是如何构造json文件:

with open(root + filename + '.txt', 'w+') as json_file:
        # convert from Python dict-like structure to JSON format
        jsoned_data = json.dumps(data)
        json_file.write(jsoned_data)
        json_file.write('\n')
这给了我

{
   indented json data
}
{
   indented json data
}
注:注意括号
[]

当您尝试读取与相同的代码结构时

with open(pathToFile) as json_file:
     json_data = json.load(json_file)
     for key in json_data:
         print key["id"]
最终会出现错误:
ValueError:额外数据:第101行第1列-第1889行第1列

我认为您应该将新的JSON数据添加到包含以前数据的列表中:

newData = {"entities": {
    "description": {
        "urls": []
    }
},
"utc_offset": -10500,
"id": 3,
"name": "Mary",
"hit_count": 554,
"private": False,
"active_last_month": False,
"location": "",
"contacted": False,
"lang": "en"
}

def readFile():
    with open('testJson', 'r') as json_file:
        json_data = json.load(json_file)
        for key in json_data:
            print key["id"]

def writeFile():
    with open('testJson', 'r') as json_file:
        oldData = json.load(json_file)
    with open('testJson', 'w+') as json_file:
        # convert from Python dict-like structure to JSON format
        data = oldData.append(newData)
        jsoned_data = json.dumps(oldData, indent=True)
        json_file.write(jsoned_data)

if __name__ == '__main__':
    readFile()
    writeFile()
    readFile()

不可能。不能盲目地附加到JSON文件而不破坏它。JSON是一种序列化数据结构,它不是纯文本。如果您想修改它,您将不得不读取文件/解析/修改/序列化/写入文件,其他一切都将是一个丑陋的黑客行为,一定会在某个时候崩溃。对于这种CRUD工作,您可能希望查看面向文档的数据库(类似于CouchDB)。带有括号和
的示例是从web下载的文件。我知道我在让你猜。你认为它来自CouchDB还是MongoDB垃圾场?它来自哪里并不重要。JSON文件是原子文件,您只能作为一个整体处理它们,或者根本不能处理它们。如果要单独处理其中的各个位,请使用为此任务制作的工具。或者使用许多小文件。@Tomalak:完全可能,只是不太理想。看见JSON行将使这更容易,在JSON文档之间添加换行符,并确保文档本身没有换行符。@Martijn但这将是一种不再是JSON的新文件格式。:)