我正在尝试将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文件。因为读取同一文件时会产生以下错误:
为什么创建的Json文件格式不正确?在
这是line
和data
的示例输出:
"[{\"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"}}......]
因为您的数据是一个字典列表,所以您需要遍历它。在
您确定文件的每一行都是有效的json吗?在
我认为这是一个正确的方法:
如果这不起作用,请尝试打印生成的json文件,以了解如何解码。在
相关问题 更多 >
编程相关推荐