报价中的引号,Google API JSON + PYTHON + POSTGREDB

时间:2018-01-26 07:19:04

标签: python json postgresql

我无法处理JSON文件中返回的引号内的引号。 Python做一件事来处理它而postgredb做另一件事。 我对如何处理它感到困惑。

说实话,我从google API获得了一个json:

    {
   "html_attributions" : [],
   "result" : {
      "address_components" : [
         {
            "long_name" : "Mt Cook National Park",
            "short_name" : "Mt Cook National Park",
            "types" : [ "locality", "political" ]
         },
         {
            "long_name" : "Canterbury",
            "short_name" : "Canterbury",
            "types" : [ "administrative_area_level_1", "political" ]
         },
         {
            "long_name" : "New Zealand",
            "short_name" : "NZ",
            "types" : [ "country", "political" ]
         },
         {
            "long_name" : "7999",
            "short_name" : "7999",
            "types" : [ "postal_code" ]
         }
      ],
      "adr_address" : "\u003cspan class=\"locality\"\u003eMt Cook National Park\u003c/span\u003e \u003cspan class=\"postal-code\"\u003e7999\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eNew Zealand\u003c/span\u003e",
      "formatted_address" : "Mt Cook National Park 7999, New Zealand",
      "formatted_phone_number" : "03-435 1653",
      "geometry" : {
         "location" : {
            "lat" : -43.7364999,
            "lng" : 170.0989493
         },
         "viewport" : {
            "northeast" : {
               "lat" : -43.7351509197085,
               "lng" : 170.1002982802915
            },
            "southwest" : {
               "lat" : -43.7378488802915,
               "lng" : 170.0976003197085
            }
         }
      },
      "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/lodging-71.png",
      "id" : "dfed4d694ea89bf6be3a352b159623d917f421c1",
      "international_phone_number" : "+64 3-435 1653",
      "name" : "Mt Cook Lodge and Motel",
      "photos" : [
         {
            "height" : 540,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/108050286121550519618/photos\"\u003eEllie Choi\u003c/a\u003e"
            ],
            "photo_reference" : "CmRaAAAAnzfg-U-7RdqayAqJB2Fru84CYNXdRQ6EmZKiBdOseZV4I3N05XO7Ehux5vp84CIegydHW0eQE1SDhlcDrBbQJM6yc-1ToMB1FCkaTqHoj6Xmka0kiRz0FjQx2TOnZf7-EhAff-lv4yV9JEWTcCEqwubyGhTBlQaxP_lqdyMQ1HsnVPKX5A90JA",
            "width" : 960
         },
         {
            "height" : 4032,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/110549331727067931089/photos\"\u003eMelody Li\u003c/a\u003e"
            ],
            "photo_reference" : "CmRaAAAA1v5WhvZx_Qjg1SFZfwR2u-jafIrCzTMWCbbiqyfX7NUiKbGPKoY0mfrYd2fQNaBzKviCTV8TEePR4vsxJQIrJ5Kvijoa4ge8Jh4morCY8XJHd3X-wZuO1qHuNs4nBl7GEhBjty3FshioZu8GsanNjKsSGhR4faq9OU5vcJ0H5vZO8C3uiXtnqA",
            "width" : 3024
         },
         {
            "height" : 2976,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/113295503239818946682/photos\"\u003eJeff\u003c/a\u003e"
            ],
            "photo_reference" : "CmRaAAAA3VCpf87699o02e-3O-59OyzWziMzEErYFDkEq_MONJjvPGYNeP6J6xQT0tfgapb-MJ_JQsYJYslh4A2yTEji5sGL-bOnR1Ghm3ivNIWUOGnVxaA1UgMR8wGC_ddUBvJqEhADVV97qhpuC3ApEnCgKxk4GhTR10JOl-7tb4sKOVHYYLl79ON2LQ",
            "width" : 3968
         },
         {
            "height" : 1080,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/100722193417268901357/photos\"\u003eSu Min\u003c/a\u003e"
            ],
            "photo_reference" : "CmRaAAAAfmUSawjs6dNubeazeo_wmadsyLNcJRogJ69STVEZquXsxY2sIDfHmtxLUqEodkqRYSPGWDX7fI-OYjrPDe0MzdnJzHJSAt7mcf1UyfHq_dZb9-teqFyqOEdeQ2z3xCDLEhDpxJwlv7D94YOCl82jwfjDGhR6XpAZnF-RwTUf1ToBE8tjGCHUAw",
            "width" : 1616
         },
         {
            "height" : 2960,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/113295503239818946682/photos\"\u003eJeff\u003c/a\u003e"
            ],
            "photo_reference" : "CmRaAAAAkWyfCqRvpb5zg07Rzy0GqL5MHQDw2i-HMdEzqw51FshZimYVx7kWmCt-bztMEuECOJkgrspQCJlSml9pcbo7nXDTI9i_7WwfMmybRzXHRmm8vYXQOFK593DiBBuj3tJUEhB1CGdwtNb2sGB7d0QYc5ntGhSPo8WBB2BBeWVSd8UHK8r2a1m_TQ",
            "width" : 3952
         },
         {
            "height" : 960,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/103513026764128366369/photos\"\u003eJack Lee\u003c/a\u003e"
            ],
            "photo_reference" : "CmRaAAAA8HqhkhHDchBhC-SvhzcwqL1aJEPs-ERft3Lw2_AQzOTngUGdyLBTurZmNcbIiJTxq9I0cSL7zZNqiwE7oBcgBMDAB5dFAE4ZQYn70Y7MlU1dta4_woEjmauqn3vRbSnyEhDuXEkf_oyBecsOEoAbBK_GGhScBrrUA10VR3AkP_9iI3q2HeTxog",
            "width" : 1706
         },
         {
            "height" : 2976,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/113295503239818946682/photos\"\u003eJeff\u003c/a\u003e"
            ],
            "photo_reference" : "CmRaAAAA7kNP6NMYhu2rAfZrmiMdWRZam_vz7p6hRljjaOVx_Dl8x5_lNBQHjE2tsysy72si8QZTFhZCg9VRQF-wcY_yUNEPezFbHhWnn2w6AJEQatbWtnhE6MiNoWHFjMwb6FBDEhAMjtxgbxPgZieonI5VXJg2GhSY_xhiQPkpY8d6dH772zJCMG1hrw",
            "width" : 3968
         },
         {
            "height" : 3096,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/112415917350560216557/photos\"\u003eAndrew Robb\u003c/a\u003e"
            ],
            "photo_reference" : "CmRaAAAARh4IVX8cuCPiHnsaGFj6VRkaoC2tSFRO-kntjDPW0uXA5YKHF0EKL1HyaN5G0pY1PPzXnnhYsqrotT2U93LgClKqKcMfwWmJxA4nombgHSdOUg7gUuAzHkD8GQYgIi-UEhCkxq7NXJ9VQ5lD6PfSOXRKGhSuzLznpizGWDM6MGaZhJXeEhz1HA",
            "width" : 4128
         },
         {
            "height" : 2268,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/101293002368238749006/photos\"\u003eCorne van der Westhuizen\u003c/a\u003e"
            ],
            "photo_reference" : "CmRaAAAAZFkPm4g055jTlt0edO-0LBZOrsh1YTh6XzuM4aJxC8o6IdhVYgDff7PYoSuZ8Pue_KCgqLq_5PVKpYiL2Ihkp0z8qBp7luwNSnmZdv3PaTQ1V7Ew2QpsLA-f9rDTPW1cEhDlwvQvrstsCgsmDd8Z-2QNGhQN_t0rCa-y-4h0ZV5Hc9JuGuF1eQ",
            "width" : 4032
         },
         {
            "height" : 1360,
            "html_attributions" : [
               "\u003ca href=\"https://maps.google.com/maps/contrib/110170972772792272403/photos\"\u003e高橋和也\u003c/a\u003e"
            ],
            "photo_reference" : "CmRaAAAAncS_1e2Pr8rPvDvMfsGc1T1i5cqdQUK3bBuZiEQ1rMd5S825xoN21WNhoZLk1544AC6euQq8oA48PcO7CrOiFTEnyewA_2T9gDHV77cRCLEGBuLlWDgp2529x3-xMhGUEhA8WIpZeFLDtcN-AarJvz2IGhQBZ5dgK31_auxdKcjlaX3zPUaI0Q",
            "width" : 2048
         }
      ],
      "place_id" : "ChIJ2fi5GK20K20Rv2BtDceLwIY",
      "rating" : 3.8,
      "reference" : "CmRSAAAA5BDF1myqhU3mXivOmf13NWZWXsm1TmXlSmqzw9ofaheM8R-NqzG9TJs0ByXsInspupJpj4X7K0pFV83ZZGVVIMuGhg1PLZQQnxVVAHw9DjiOau4ZIY4SPk_ZbcrSPTlGEhAlNyhhsBaVdyRjkqniY89aGhTD2Kor8gIsnFzk-ud25EXMPfSwOw",
      "reviews" : [
         {
            "author_name" : "Kim-Yen Nguyen",
            "author_url" : "https://www.google.com/maps/contrib/107238835762969885704/reviews",
            "language" : "en",
            "profile_photo_url" : "https://lh5.googleusercontent.com/-z9PWZ-QoJxQ/AAAAAAAAAAI/AAAAAAAABG0/wNOHWLI6P68/s128-c0x00000000-cc-rp-mo-ba2/photo.jpg",
            "rating" : 4,
            "relative_time_description" : "2 weeks ago",
            "text" : "We loved the stay at the motel room, which was spacious, clean, we even had a back yard. // Then the stay at the lodge was only so so, cleanliness could have been better, the general state of the bathroom was not superb. Overall satisfied with Mt Cook Lodge and Motel, but would definitely recommend staying at a motel room if budget allows.",
            "time" : 1514865875
         },
         {
            "author_name" : "Ben Lilburne",
            "author_url" : "https://www.google.com/maps/contrib/109755001469843583867/reviews",
            "language" : "en",
            "profile_photo_url" : "https://lh6.googleusercontent.com/-HdZVXck6Kek/AAAAAAAAAAI/AAAAAAAAABQ/v1vfaukWe5o/s128-c0x00000000-cc-rp-mo/photo.jpg",
            "rating" : 5,
            "relative_time_description" : "a month ago",
            "text" : "Had a motel room at the bottom of the hill, it's a beautiful area, but is very busy at times as tour buses come through. The rooms are spacious and quiet, great views from anywhere here. It's a short walk up the hill on a gravel path to the main place where tours go from.",
            "time" : 1511721269
         },
         {
            "author_name" : "Lucas Jewett",
            "author_url" : "https://www.google.com/maps/contrib/110030254920960945419/reviews",
            "language" : "en",
            "profile_photo_url" : "https://lh5.googleusercontent.com/-I7Ps4CSwGnU/AAAAAAAAAAI/AAAAAAAAAAA/AA6ZPT5LvSLYc7_r9mkmF96YB9hG7LVzGg/s128-c0x00000000-cc-rp-mo/photo.jpg",
            "rating" : 4,
            "relative_time_description" : "a month ago",
            "text" : "Great rooms. No AC just window's. The couch was a fold out bed as well. You can easily hear people above you walking around as well.",
            "time" : 1511892349
         },
         {
            "author_name" : "Nicholas Poh",
            "author_url" : "https://www.google.com/maps/contrib/117169519963119381503/reviews",
            "language" : "en",
            "profile_photo_url" : "https://lh5.googleusercontent.com/-IQzeAxgoa4Y/AAAAAAAAAAI/AAAAAAACAJg/QusM_WIUXrQ/s128-c0x00000000-cc-rp-mo/photo.jpg",
            "rating" : 4,
            "relative_time_description" : "2 weeks ago",
            "text" : "Room was good. Restaurant's view is awesome. There's a communal kitchen for guest use.",
            "time" : 1515125180
         },
         {
            "author_name" : "Curtis Mohrhardt",
            "author_url" : "https://www.google.com/maps/contrib/105675185047970962389/reviews",
            "language" : "en",
            "profile_photo_url" : "https://lh3.googleusercontent.com/-E8RLgnnD15g/AAAAAAAAAAI/AAAAAAAAAC0/kJZ1Z9BLmx8/s128-c0x00000000-cc-rp-mo/photo.jpg",
            "rating" : 5,
            "relative_time_description" : "3 weeks ago",
            "text" : "We were put in one of the newer cabins, it was very clean and enough room for a family of four. Short drive to the hiking trails. Great place.",
            "time" : 1514529120
         }
      ],
      "scope" : "GOOGLE",
      "types" : [ "lodging", "point_of_interest", "establishment" ],
      "url" : "https://maps.google.com/?cid=9709914483650814143",
      "utc_offset" : 780,
      "vicinity" : "Mt Cook National Park",
      "website" : "http://mtcooklodge.co.nz/"
   },
   "status" : "OK"
}

现在的问题是,我想让它直接进入我的POSTGREDB的JSON格式列。 POSTGREDB非常适合处理JSON。

我尝试做的是逃避此API中的单引号...

但这意味着POSTGREDB会弹出错误。

我生气了。我尝试了以下方法:

URL='https://maps.googleapis.com/maps/api/place/details/json?placeid='+rows[0]+'&key=Something something'
    file = urllib.request.urlopen(URL)
    file1=file.read()
    file2=json.loads(file1)
    file3=json.dumps(file2)
    query='update public.attraction set g_json_1=\'%s\' where google_place_id="%s"'% (file3,str(rows[0]))
    print(query)
    cursor.execute(query)

1 个答案:

答案 0 :(得分:0)

我认为在Postgres中,你可以通过加倍'

来逃避单引号''

因此,您可能需要在json内容中将'替换为''

URL='https://maps.googleapis.com/maps/api/place/details/json?placeid='+rows[0]+'&key=Something something'
file = urllib.request.urlopen(URL)
file1=file.read()
file2=json.loads(file1)
file3=json.dumps(file2)
file3 = file3.replace("'", "''")  # escape single quotes
query='update public.attraction set g_json_1=\'%s\' where google_place_id="%s"'% (file3,str(rows[0]))
print(query)
cursor.execute(query)  

我希望它有所帮助