简体   繁体   English

在python中将多个列表写入JSON文件

[英]Write multiple lists to a JSON file in python

Assume I have the following lists假设我有以下列表

list1 = [{"created_at": "2012-01-31T10:00:04Z"},{"created_at": "2013-01-31T10:00:04Z"}] 
list2 = [{"created_at": "2014-01-31T10:00:04Z"}] 

I can write the first list to a JSON file using json.dump(list1,file,indent=2) and the result is我可以使用json.dump(list1,file,indent=2)将第一个列表写入 JSON 文件json.dump(list1,file,indent=2)结果是

[
  {
    "created_at": "2012-01-31T10:00:04Z"
  },
  {
    "created_at": "2013-01-31T10:00:04Z"
  }
]

My question is, how do I append the contents of the second list?我的问题是,如何附加第二个列表的内容? if I simple do json.dump(list2,file,indent=2) , it results in an invalid JSON file as below.如果我简单地做json.dump(list2,file,indent=2) ,它会导致一个无效的 JSON 文件,如下所示。

[
  {
    "created_at": "2012-01-31T10:00:04Z"
  },
  {
    "created_at": "2013-01-31T10:00:04Z"
  }
][
  {
    "created_at": "2014-01-31T10:00:04Z"
  }
]

Edit : The lists are created dynamically by parsing about 8000 files.编辑:列表是通过解析大约 8000 个文件动态创建的。 The above lists are just example.上面的列表只是示例。 I could potentially be writing 8000 lists to the JSON file, so simple appending will not work.我可能会向 JSON 文件写入 8000 个列表,因此简单的追加将不起作用。

In [1]: import json

In [2]: list1 = [{"created_at": "2012-01-31T10:00:04Z"},{"created_at": "2013-01-31T10:00:04Z"}] 

In [3]: list2 = [{"created_at": "2014-01-31T10:00:04Z"}] 

In [4]: list1.extend(list2)

In [5]: json.dumps(list1)
Out[5]: '[{"created_at": "2012-01-31T10:00:04Z"}, {"created_at": "2013-01-31T10:00:04Z"}, {"created_at": "2014-01-31T10:00:04Z"}]'

or

In [8]: json.dumps(list1 + list2)
Out[8]: '[{"created_at": "2012-01-31T10:00:04Z"}, {"created_at": "2013-01-31T10:00:04Z"}, {"created_at": "2014-01-31T10:00:04Z"}]'

When parse the files append (or extend) to a unique list and finally convert to JSON.解析文件时,附加(或扩展)到一个唯一的列表并最终转换为 JSON。 Assume that your function for parse is parse .假设您的 parse 函数是parse

>>> import json
>>> result = []
>>> for file in files:
...     result.append(parse(file))
...
>>> json.dump(result, file1, indent=2)

I found a little lacking in the explanation given below that's why trying to make a point considered over here.我发现下面给出的解释有点缺乏,这就是为什么试图在这里考虑一个观点。 A Json file can have a single parent element.一个Json文件可以有一个单一的父元素。 Therefore, if at the first iteration, you dump 1st list then at the 2nd iteration, you will get the formatting error in the file.因此,如果在第一次迭代时转储第一个列表,然后在第二次迭代时,您将在文件中得到格式错误。 B/c Json demands these two lists to be wrapped inside one list/array before dumping. B/c Json 要求在转储之前将这两个列表包装在一个列表/数组中。

Therefore, you store all lists in one list (either using appending or any other above-mentioned methods).因此,您将所有列表存储在一个列表中(使用附加或任何其他上述方法)。 And then you dump this aggregated list into your Json file.然后你将这个聚合列表转储到你的Json文件中。 However, if you do not want to do so, you will have to create different files for your different lists.但是,如果您不想这样做,则必须为不同的列表创建不同的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM