2

我有一个字符串:

mydata
'POINT (558750.3267372231900000 6361788.0628051758000000)'

我希望有一种代码保存方式将列表数字转换为

(g, (x,y)) 

在哪里:

g = geometry (POINT)
x = coordinates x
y = coordinates y

我在用

mydata.split(" ")
['POINT', '(558750.3267372231900000', '6361788.0628051758000000)']

但在那之后我需要使用几行代码来获取 x 和 y

4

8 回答 8

3

一步步:

>>> s = 'POINT (558750.3267372231900000 6361788.0628051758000000)'
>>> word, points = s.split(None, 1)
>>> word
'POINT'
>>> points
'(558750.3267372231900000 6361788.0628051758000000)'
>>> points = points.strip('()').split()
>>> points
['558750.3267372231900000', '6361788.0628051758000000']
>>> x, y = (float(i) for i in points)
>>> x
558750.3267372232
>>> y
6361788.062805176
于 2012-12-06T17:47:31.307 回答
3

正则表达式可以省去你在这里打字的时间:

In [1]: import re

In [2]: def nice_tuple(s):                                                    
    g, x, y, _ = re.split(' ?[()]?', s)
    return g, tuple(map(float, (x, y)))
   ...: 

In [3]: nice_tuple('POINT (558750.3267372231900000 6361788.0628051758000000)')
Out[3]: ('POINT', (558750.3267372232, 6361788.062805176))
于 2012-12-06T17:50:04.347 回答
2

如果您的数据始终采用该精确格式,则很容易:

>>> def parse_data(d):
    geom, xs, ys = d.split()
    return (geom, (float(xs[1:]), float(ys[:-1])))

>>> mydata
'POINT (558750.3267372231900000 6361788.0628051758000000)'
>>> parse_data(mydata)
('POINT', (558750.32673722319, 6361788.0628051758))
于 2012-12-06T17:52:49.727 回答
1

我会使用.translate.split

In [126]: mydata = 'POINT (558750.3267372231900000 6361788.0628051758000000)'

In [127]: mysplitdata = mydata.translate(None, '()').split()

In [128]: mysplitdata
Out[128]: ['POINT', '558750.3267372231900000', '6361788.0628051758000000']

In [129]: g,x,y = mysplitdata[0],float(mysplitdata[1]),float(mysplitdata[2])

In [130]: outdata = (g, (x,y))

In [131]: outdata
Out[131]: ('POINT', (558750.32673722319, 6361788.0628051758))
于 2012-12-06T18:00:12.953 回答
1

最近我在 python 中创建了一个应用程序,我做了几乎同样的事情。这是我为解析 wkt 文件而创建的一个类。

关联

希望你觉得它有用。有关用法,请参见第 136 行。您也可以使用此类来读取 Linestrings 和 Multilinestrings。

于 2012-12-06T18:02:17.677 回答
1
v = mydata.split()
g = v[0]
x = float(v[1].strip('('))
y = float(v[2].strip(')'))
(g, (x, y))

节省代码是的,优雅不是那么多

于 2012-12-06T17:45:11.850 回答
1

使用regex

In [59]: g,[x,y]=re.findall(r"[A-Za-z]+",mydata)[0],
                       [float(x) for x in re.findall(r"[\d+.]+",mydata)]

In [60]: g
Out[60]: 'POINT'

In [61]: x
Out[61]: 558750.3267372232

In [62]: y
Out[62]: 6361788.062805176

使用str.strip()str.split()

In [35]: mydata='POINT (558750.3267372231900000 6361788.0628051758000000)'

In [39]: data=mydata.split(None,1)

In [40]: data
Out[40]: ['POINT', '(558750.3267372231900000 6361788.0628051758000000)']

In [41]: g,[x,y]=data[0], map(lambda x: float(x.strip("()")), data[1].split())

In [42]: g,x,y
Out[42]: ('POINT', 558750.3267372232, 6361788.062805176)
于 2012-12-06T17:46:13.770 回答
1
found = re.match(r'([a-zA-Z]*) \(([0-9\.]*) ([0-9\.]*)\)', mydata)
found.group(1), (float(found.group(2)), float(found.group(3)))

那可能是最短的,不知道优雅。

于 2012-12-06T17:56:30.220 回答