首页 > 解决方案 > IndentationError:预期缩进块MongoDB

问题描述

我正在尝试将 json 文件导入 MangoDB,但出现错误:

File "<ipython-input-2-64b0e1ec4bba>", line 13
    data = json.load(f)
    ^
IndentationError: expected an indented block

我的代码是:

import json, pymongo
from pymongo import MongoClient

cluster = MongoClient('mongodb+srv://Rokaya:140296@cluster0.mc9wz.mongodb.net/Hotel_Test?retryWrites=true&w=majority')
db = cluster['Hotel_Test']
collection = db['Hotel_Reviews_Test']

with open('/Users/rokayadarai/Desktop/Coding/DataSets/Hotel_Reviews.json') as f:
data = json.load(f)
collection.insert_many(data)
cluster.close()

我究竟做错了什么?我没看到什么吗?

标签: pythonmongodb

解决方案


with open('/Users/rokayadarai/Desktop/Coding/DataSets/Hotel_Reviews.json') as f:
   data = json.load(f)
   collection.insert_many(data)
   cluster.close()

您需要像上面那样缩进代码,因为您使用过“with”python 需要缩进,因此它知道如何处理您打开的 .json 文件。


推荐阅读