将MongoDB集合转换为Json文件并从Python中的同一Json文件中读取

2024-05-15 01:25:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将mongodb集合转换为json文件,稍后将相同的json文件数据加载到另一个mongodb集合。该集合大约有60000行。我写了以下代码:

from pymongo import MongoClient
import json
from bson.json_util import dumps
from bson import json_util

with open("collections/review.json", "w") as f:
    l = list(reviews_collection.find())  
    json.dump(json.dumps(l,default=json_util.default),f,indent = 4)

# reviews_collection_bkp.remove()
reviews_collection_bkp.remove()
with open("collections/review.json") as dataset:
    for line in dataset:
            data = json.loads(line)
            reviews_collection_bkp.insert({
                 "reviewId": data["reviewId"],
                 "business": data["business"],
                 "text": data["text"],
                 "stars": data['stars'],
                 "votes":data["votes"]
             })
print reviews_collection_bkp.find().count() 

review_collection是我想在Json文件名review.json中写入的集合,然后想从同一个文件中读取数据以将数据插入MongoDB集合中。但是我认为代码不能创建一个合适的json文件。因为读取同一文件时会产生以下错误:

^{pr2}$

为什么创建的Json文件格式不正确?在

这是linedata的示例输出:

"[{\"votes\": {\"funny\": 0, \"useful\": 0, \"cool\": 0}, \"business\": \"wqu7ILomIOPSduRwoWp4AQ\", \"text\": \"Went for breakfast on 6/16/14. We received very good service and meal came within a few minutes.Waitress could have smiled more but was friendly. \\nI had a Grand Slam... it was more than enough food. \\nMeal was very tasty... We will definitely go back. \\nIt is a popular Denny's.\", \"reviewId\": \"0GS3S7UsRGI4B7ziy4cd7Q\", \"stars\": 4, \"_id\": {\"$oid\": \"5711d16fe396f81fcb51dc73\"}},...]

[{"votes": {"funny": 0, "useful": 0, "cool": 0}, "business": "wqu7ILomIOPSduRwoWp4AQ", "text": "Went for breakfast on 6/16/14. We received very good service and meal came within a few minutes.Waitress could have smiled more but was friendly. \nI had a Grand Slam... it was more than enough food. \nMeal was very tasty... We will definitely go back. \nIt is a popular Denny's.", "reviewId": "0GS3S7UsRGI4B7ziy4cd7Q", "stars": 4, "_id": {"$oid": "5711d16fe396f81fcb51dc73"}}......]

Tags: 文件textimportjsondatabusinessreviewcollection
2条回答

因为您的数据是一个字典列表,所以您需要遍历它。在

for line in dataset:
    data = json.loads(line)
    for doc in data:
         reviews_collection_bkp.insert({
                 "reviewId": data["reviewId"],
                 "business": data["business"],
                 "text": data["text"],
                 "stars": data['stars'],
                 "votes":data["votes"]
             }) 

您确定文件的每一行都是有效的json吗?在

我认为这是一个正确的方法:

with open("collections/review.json") as dataset:
    data = json.loads(dataset)
    for line in data:
        reviews_collection_bkp.insert({
             "reviewId": line['reviewId'],
             ...
         })

如果这不起作用,请尝试打印生成的json文件,以了解如何解码。在

相关问题 更多 >

    热门问题