【问题标题】:Extract certain data from text file - string manipulation从文本文件中提取某些数据 - 字符串操作
【发布时间】:2021-04-28 16:49:12
【问题描述】:

我正在尝试使用 python 处理一个文本文件并提取 Lat/Lon 的所有实例,但是自从我使用它已经很多年了,我什至不知道该谷歌寻求帮助。在下面的示例中,我想要每个 start_location 和 end_location 的位置,然后将该数据放入单独的文件中。

           ""start_address"" : ""Aberdeen, SD 57401, USA"",
               ""start_location"" : {
                  ""lat"" : 45.4646954,
                  ""lng"" : -98.48596610000001
               },
               ""steps"" : [
                  {
                     ""distance"" : {
                        ""text"" : ""207 ft"",
                        ""value"" : 63
                     },
                     ""duration"" : {
                        ""text"" : ""1 min"",
                        ""value"" : 13
                     },
                     ""end_location"" : {
                        ""lat"" : 45.4641268,
                        ""lng"" : -98.48597479999999
                     },
                     ""html_instructions"" : ""Head \u003cb\u003esouth\u003c/b\u003e on \u003cb\u003eCourt St\u003c/b\u003e toward \u003cb\u003e1st Ave SE\u003c/b\u003e"",
                     ""polyline"" : {
                        ""points"" : ""kyntGhpbxQhA?f@?""
                     },

【问题讨论】:

  • 为什么你的引号加倍了?
  • 这就是它从谷歌方向 API 中下拉的方式
  • 我使用了 Google 路线 API,并且一直得到有效的 JSON 响应。加倍必须在你的最后。

标签: python json regex python-re


【解决方案1】:

看来这对你有帮助:http://fredgibbs.net/tutorials/extract-geocode-placenames-from-text-file.html

正如您在链接中看到的那样,该人获得了一些格式与您类似的坐标,正如我在此处粘贴的那样:

.
.
.
   ],
     "formatted_address" : "Albuquerque, NM, USA",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 35.2180539,
              "lng" : -106.4711629
           },
           "southwest" : {
              "lat" : 34.9467659,
              "lng" : -106.881796
           }
        },
        "location" : {
           "lat" : 35.110703,
           "lng" : -106.609991
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 35.2180539,
              "lng" : -106.4711629
           },
           "southwest" : {
              "lat" : 34.9467659,
              "lng" : -106.881796
           }           
.
.
.

然后将其作为 JSON 处理:

json = r.json()
lat = json['results'][0]['geometry']['location']['lat']
lng = json['results'][0]['geometry']['location']['lng']

注意 此人向 Google 请求了坐标,但可以针对您的问题复制此坐标

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    相关资源
    最近更新 更多