代码之家  ›  专栏  ›  技术社区  ›  Ofer Steinberg

从python中的gmaps库将集合导入mongodb服务器

  •  -1
  • Ofer Steinberg  · 技术社区  · 10 年前

    我正在使用python,并尝试导入一个JSON,就像我从下面的代码中看到的那样:

    from gmaps import Geocoding
    api = Geocoding(api_key='<my key>')
    api.geocode("calle tigre 129 cusco")
    

    到mongodb服务器,就像我用以下代码创建了一个集合:

    from pymongo import MongoClient
    client = MongoClient('mongodb://<user>:<pass>@ds049219.mongolab.com:49219/<__>')
    
    db = client.<__>
    posts = db.post
    post = [{'name': 'Joy', 'food': 'pasta'}, {'name': 'Hant', 'food': 'pizza', 'location': 'Holland'}, {'name': 'Jim', 'food': 'meat'}]
    
    id_<__>s= posts.insert(post)
    print 'create the id: %s'%post
    client.close()
    

    所以我写了这段代码:

    import pymongo
    from pymongo import MongoClient
    from gmaps import Geocoding
    
    api = Geocoding(api_key='<my key>')
    api.geocode("calle tigre 129 cusco")
    
    client2 =  MongoClient('mongodb://<user>:<pass>@ds049219.mongolab.com:49219/<__>')
    db = client2.<__>
    apis = db.api
    api = [{"calle tigre 129 cusco"}]
    
    maps_<__> = apis.insert(api)
    print 'create the maps: %s'%api
    
    client2.close()
    

    我在最后一段代码中做错了什么?

    非常感谢。

    1 回复  |  直到 10 年前
        1
  •  1
  •   Bernie Hackett    10 年前

    您的问题是这行:

    api = [{"calle tigre 129 cusco"}]
    

    {“calle tigre 129 cusco”}创建一个集合:

    >>> {"calle tigre 129 cusco"}
    set(['calle tigre 129 cusco'])
    

    PyMongo需要一个python字典。所以你需要这样的东西:

    api = [{"address": "calle tigre 129 cusco"}]