繁体   English   中英

位置,经纬度,使用 python 解析

[英]Location, lon lat, parsing with python

我有一些字符串格式的 lon lat 点。 下面的示例数据。

在此处输入图像描述

现在,我想使用这些并将它们放入两个不同的列表中,分别用于 lon 和 lat。

但是,这些数据都是字符串格式,在单个列表结构中。 我认为这意味着我需要使用正则表达式?

我试过使用下面的代码,

re.sub("^(\+|-)?(?:90(?:(?:\.0{1,6})?)|(?:[0-9]|[1-8][0-9])(?:(?:\.[0-9]{1,6})?))$"
,"",exampledatalatlon[1])

但我无法让它工作。 鉴于我已经尝试过这些方法,有谁知道将其重新格式化为 integer 格式(int() 不起作用)的快速简便的方法?

干杯

example = '(150.11, 33.20)'
lat, lon = map( lambda x: int(float(x)), example[1:-1].split(',') )

>>> lat = 150
>>> lon = 33
import re
input_string = """
(150.32424, -234.4234242)
(242.42342, -42342.4242)
(-2424, 2424)
"""
result = re.findall(r"\((-?\d+(?:\.\d+)?), *(-?\d+(?:\.\d+)?)\)", input_string)

# Use this if you just want the integer parts
# result = re.findall(r"\((-?\d+)(?:\.\d+)?, *(-?\d+)(?:\.\d+)?\)", input_string)

long, lat =  zip(*map(lambda pair: map(float, pair), result))

print(long) 
(150.32424, 242.42342, -2424.0)

print(lat)
(-234.4234242, -42342.4242, 2424.0)

什么迈克尔。 让我知道这是否是您想要的。 此脚本获取您的输入字符串并将假定的数据点分隔到单独的列表中,并将它们转换为浮点数。

'''Beaufuh 
Follow me on twitter @Beaufuhh
'''

#given data
location = '''(150. 90173422372985, -33. 668617545658414) 
(150.90125083937394,-33.6680017272324) 
(150.8952219040607,-33.69772659617821)
(150. 8495268692736, -33. 63824270203117) 
(150. 8940727 4513587, -33. 70546707 420955) 
(150. 8941156604801, -33. 70485201015482) 
(150. 89314185096964, -33. 714339905449854) 
(150.84348234362048,-33.63098091448995)
(150.8488030081157,-33.63777549474839)
(150. 837384089968, -33. 6256979266494 7)
(150. 958490613542, -33. 716626620381064)
(150. 89336724034592, -33. 712682761283325) 
(150.92057699351656,-33.68832356765278)
(150.92020399882543,-33.687884523564655) 
(150. 89387233383104, -33. 70732890445153) 
(150.83819855949918,-33.62531118563714) 
(150. 843766490138, -33. 631440326602785) 
(150.83880163743237,-33.626027838358304) '''
#create a list from location string and separate by new line
locations = location.split('\n')
#initialize list to hold longitudes and latitudes
longitudes = []
latitudes = []
#list iteration
for i in locations:
    #split each line by comma to seperate long and lat.
    i_split_by_comma = i.split(',')
    #list iteration
    for j in i_split_by_comma:
        #check if longitude
        if '(' in j:
            #replace paren
            j = j.replace('(', '')
            #replace space after decimal
            j = j.replace(' ','')
            #convert to float
            j = float(j)
            #append to new list
            longitudes.append(j)
        elif ')' in j:
            #replace paren
            j = j.replace(')', '')
            #replace space after decimal
            j = j.replace(' ','')
            #convert to float
            j = float(j)
            #append to new list
            latitudes.append(j)
        else:
            print('Something funky here. Inspection needed.')
#print new lists
print(longitudes, '\n\n\n\n', latitudes)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM