我有一个字符串:
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
最佳答案
一步一步地:
>>> 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
关于python - Python:在列表中拆分字符串的优雅且节省代码的方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13749324/