位置,lon-lat,用python解析

2024-09-28 21:00:40 发布

您现在位置:Python中文网/ 问答频道 /正文

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

enter image description here

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

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

我试着使用下面的代码

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

但我不能让它工作。如果我已经尝试过这些方法,有没有人知道一种快速简单的方法可以将其重新格式化为整数格式(int()不起作用)

干杯


Tags: 数据方法字符串代码re示例列表格式
3条回答
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)

相关问题 更多 >