[英]Add line number and count amount with python
我有一個來自地理信息系統的文件,我需要該文件的一部分才能在arcgis中使用並在其中創建功能。
這是該文件的一部分:
<PropertyValuePair property="LineControlCategory" value="BDYOR" />
<PropertyValuePair property="Location" value="|0|LINESTRING (-64.9734423 46.8886667 0, -64.975073 46.9527568 0, -65.015054 46.9900623 0, -65.0746205 47.0712622 0, -65.0990984 47.0740409 0, -65.1456079 47.0551434 0, -65.1505056 47.0451361 0, -65.2100694 47.0445795 0, -65.2010947 47.0712622 0, -65.204359 47.0834863 0, -65.2206776 47.0834863 0, -65.2476043 47.0740409 0, -65.2720821 47.0829311 0, -65.2916649 47.066815 0, -65.3357282 47.0801523 0, -65.3732604 47.0773749 0, -65.4219264 47.0582964 0)" />
<PropertyValuePair property="ObjectLabelA" value="13" />
<PropertyValuePair property="LineControlCategory" value="BDYOR" />
<PropertyValuePair property="Location" value="|0|LINESTRING (-65.4401144 46.9992797 0, -65.4268321 47.0112236 0, -65.4351497 47.030746 0, -65.4126433 47.0448317 0, -65.4213041 47.0585458 0)" />
<PropertyValuePair property="ObjectLabelA" value="para mil force" />
我現在擁有的python腳本給我:
X,Y,Feature,FeatureOrder
-64.9734423,46.8886667,
-64.975073,46.9527568,
-65.015054,46.9900623,
-65.0746205,47.0712622,
-65.0990984,47.0740409,
我需要使其真正在arcgis中工作,我需要行號和訂單號。 所以像這樣:
X,Y,Feature,FeatureOrder
-64.9734423,46.8886667,1,1
-64.975073,46.9527568,1,2
-65.015054,46.9900623,1,3
Etc
-65.4401144,46.9992797,2,1
-65.4268321,47.0112236,2,2
-65.4351497 47.030746,2,3
-65.4126433,47.0448317,2,4
第一個數字1來自第一行,其中包含“ Location”。 第二個數字是該行中數字范圍的順序。 這是我現在擁有的腳本:
f = open('OVERLAY.ovl','r')
file = open("newfile.txt", "w")
file.write("X,Y,Feature,FeatureOrder\n")
for line in f.readlines():
if "Location" in line:
file.write(line.split("(")[1].split(")")[0].replace(", ", "\n").replace(" 0", ",\n").replace(" ", ","))
f.close()
file.close()
我希望有人可以幫我打招呼彼得
給定代碼,可能很難實現。 線
file.write(line.split("(")[1].split(")")[0].replace(", ", "\n").replace(" 0", ",\n").replace(" ", ","))
是無法讀取的單行代碼。 至少應重構為
content = line.split("(")[1].split(")")[0]
rows = [y.replace(" ", ",") for x in content.split(', ')
for y in x.split(' 0')]
file.writelines(rows)
仍然很臟,但是足夠干凈以更改代碼行為:
for i, line in enumerate(f.readlines(), 1):
if "Location" in line:
content = line.split("(")[1].split(" 0)")[0]
rows = [y.replace(" ", ",") for x in content.split(', ')
for y in x.split(' 0')
if y != '']
file.writelines(['{},{},{}\n'.format(row,i,j)
for j, row in enumerate(rows, 1)])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.