[英]Parsing a string into a list of dicts
我有一個看起來像這樣的字符串:
POLYGON ((148210.445767647 172418.761192525, 148183.930888667 172366.054787545, 148183.866770629 172365.316772032, 148184.328078148 172364.737139913, 148220.543522168 172344.042601933, 148221.383518338 172343.971823159), (148221.97916844 172344.568316375, 148244.61381946 172406.651932395, 148244.578100039 172407.422441673, 148244.004662562 172407.938319453, 148211.669446582 172419.255646473, 148210.631989339 172419.018894911, 148210.445767647 172418.761192525))
我可以輕松地從字符串中刪除POLYGON
來關注數字,但是我有點想知道將字符串解析為字典列表的最簡單/最佳方法是什么。
第一個括號(POLYGON后右)表示多個元件可被提供(由逗號分隔,
)。
因此,每對數字應該是x
和y
。
我想解析此字符串以得到以下數據結構(使用python 2.7
):
list [ //list of polygons
list [ //polygon n°1
dict { //polygon n°1's first point
'x': 148210.445767647, //first number
'y': 172418.761192525 //second number
},
dict { //polygon n°1's second point
'x': 148183.930888667,
'y': 148183.930888667
},
... // rest of polygon n°1's points
], //end of polygon n°1
list [ // polygon n°2
dict { // polygon n°2's first point
'x': 148221.9791684,
'y': 172344.568316375
},
... // rest of polygon n°2's points
] // end of polygon n°2
] // end of list of polygons
多邊形的點數實際上是無限的。
每個點的數字用空格分隔。
你們知道循環執行此方法還是任何遞歸方法?
PS:我是一個python初學者(僅幾個月的經驗),所以請隨時詳細解釋。 謝謝!
定義Polygon對象的數據結構看起來非常類似於python元組聲明。 一個選項(盡管有點棘手)是使用python的AST解析器 。
您將不得不剝離POLYGON零件,並且該解決方案可能不適用於更復雜的其他聲明。
import ast
your_str = "POLYGON (...)"
# may be better to use a regex to split off the class part
# if you have different types
data = ast.literal_eval(your_str.replace("POLYGON ",""))
x, y = data
#now you can zip the two x and y pairs together or make them into a dictionary
假設您有一個看起來像這樣的字符串
my_str = 'POLYGON((148210.445767647 172418.761192525,148183.930888667 172366.054787545,148183.866770629 172365.316772032,148184.328078148 172364.737139913,148220.543522168 172344.042601933,148221.383518338 172343.971823159),(148221.97916844 172344.568316375,148244.61381946 172406.651932395,148244.578100039 172407.422441673,148244.004662562 172407.938319453,148211.669446582 172419.255646473,148210.631989339 172419.018894911,148210.445767647 172418.761192525))'
my_str = my_str.replace('POLYGON ', '')
coords_groups = my_str.split('), (')
for coords in coords_groups:
coords.replace('(', '').replace(')', '')
coords_list = coords.split(', ')
coords_list2 = []
for item in coords_list:
item_split = item.split(' ')
coords_list2.append({'x', item_split[0], 'y': item_split[1]})
我認為這應該有所幫助
您現在需要的是在括號之間獲取信息的方法,這應該有助於正則表達式在括號之間返回文本
UPDATE更新上面的感謝代碼由另一個答案https://stackoverflow.com/users/2635860/mccakici ,但是這只有當u有串的結構為u你的問題說
你能試一下嗎?
import ast
POLYGON = '((148210.445767647 172418.761192525, 148183.930888667 172366.054787545, 148183.866770629 172365.316772032, 148184.328078148 172364.737139913, 148220.543522168 172344.042601933, 148221.383518338 172343.971823159), (148221.97916844 172344.568316375, 148244.61381946 172406.651932395, 148244.578100039 172407.422441673, 148244.004662562 172407.938319453, 148211.669446582 172419.255646473, 148210.631989339 172419.018894911, 148210.445767647 172418.761192525))'
new_polygon = '(' + POLYGON.replace(', ', '),(').replace(' ', ',') + ')'
data = ast.literal_eval(new_polygon)
result_list = list()
for items in data:
sub_list = list()
for item in items:
sub_list.append({
'x': item[0],
'y': item[1]
})
result_list.append(sub_list)
print result_list
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.