I am getting this error while trying to import:
Traceback (most recent call last):
File "C:\PythonSC\module21.py", line 8, in <module>
page = open('c:\restaurants\restaurants.json','r')
OSError: [Errno 22] Invalid argument: 'c:\restaurants\restaurants.json'
here is my source code of trying to import:
import json
import pymongo
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client.restaurants
collection = db.restaurantsCollection
page = open('c:\restaurants\restaurants.json','r')
dataset = json.loads(page.read())
for item in dataset:
collection.insert(item)
for item in collection.find():
print(item)
and here is the sample json data from https://raw.githubusercontent.com/mongodb/docs-assets/geospatial/restaurants.json
I still could not understand why this error and I hope someone could lend me a helping hand. Thanks a lot.
'c:\restaurants\restaurants.json'->r'c:\restaurants\restaurants.json'? It's possible it wants to read backslashes as escape characters.rmakes the string "raw", so you don't have to escape backslashes themselves. Always provide regexes and paths as raw strings. And passwords, if entered in the code or the console if entered as a string.