簡體   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