缩进错误:应为缩进块MongoDB

2024-09-27 17:48:01 发布

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

我试图将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()

我做错了什么?我没看见什么吗


Tags: 文件testimportjsondbdatamongodbload
1条回答
网友
1楼 · 发布于 2024-09-27 17:48:01
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文件

相关问题 更多 >

    热门问题