python - 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()
我究竟做错了什么?我没看到什么吗?
解决方案
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 文件。
推荐阅读
- maven - 如何在 Artifactory OSS 版本上设置复制
- c# - ASP.NET Core 2.1 - 在没有反射的情况下运行属性的代码
- angular - BehaviorSubject 订阅者多次获得相同的 next() 元素
- java - 用于实时监控应用程序的 AWS 云手表
- react-native - 使用 Expo 反应本机指南针
- spring - 如何连接 Spring Cloud Gateway + Spring Cloud Security + Keycloak
- python - 使用 python elasticsearch 在 AWS Elasticsearch 上搜索失败返回 411
- gradle - 将 gradle 项目部署到 Artifactory
- django - Django 2.0 Queryset 注释日期
- javascript - Webpack - 登录和分析