我试图将一串坐标“(x1,y1),(x2,y2)”变成[[x1,y1],[x2,y2]],其中所有坐标也被转换为浮点数。
我当前的代码似乎没有修剪y坐标的空格,也没有将字符串转换为浮点数。
coordinates = "(-89.38477, 26.6671), (-89.38477, 27.13737), (-88.81348, 27.13737), (-88.81348, 26.6671)"
for x in re.findall("\((.*?)\)", coordinates):
final_coordinates = x.lstrip().split(',')
答案 0 :(得分:4)
一种选择是将coordinates
括在方括号中并使用literal_eval()
:
>>> from ast import literal_eval
>>> coordinates = "(-89.38477, 26.6671), (-89.38477, 27.13737), (-88.81348, 27.13737), (-88.81348, 26.6671)"
>>>
>>> data = list(literal_eval(coordinates))
>>> data
[(-89.38477, 26.6671), (-89.38477, 27.13737), (-88.81348, 27.13737), (-88.81348, 26.6671)]