2016-07-18 108 views
-2

一個文本文件,這是我的代碼:格式化在Python

import arcpy 
arcpy.env.workspace = "C:\Users\Brett\Desktop\lesson_6\Lesson6_Data" 
infc = "Cities.shp" 

outputFile = open("C:\Users\Brett\Desktop\lesson_6\Lesson6_Data\Output_Cities1.txt", "w")  

arcpy.GetParameterAsText(0) 

fc = "Cities.shp" 
fields = ["NAME","[email protected]"] 

with arcpy.da.SearchCursor(fc, fields) as cursor: 
for row in cursor: 
    outputFile.write('{0}, {1}'.format(row[0],(row[1]))) 

print "done" 

這是輸出的格式如下:

海華沙,(-1050316.3479999993,2067521.4093999993)粉末洗淨,(-1025371.6007000003,2059421.7783000004 )國王峽谷,(-852695.0120999999,2036738.5595999993)科倫(-915047.0152000003,2035509.35099999

但我希望它看起來像這樣:

FredRanch1_1, 529018.125025, 4108038.05548 
FredRanch1_1, 529005.718792, 4108028.20659 
FredRanch1_1, 528993.340503, 4108018.73931 
FredRanch1_1, 528980.990158, 4108009.65364 
FredRanch1_1, 528968.667757, 4108000.94958 

etc ....

關於如何格式化的任何建議是否正確?

回答

1

[email protected]返回值的元組,您可以通過它們的索引位置訪問它。您可以通過將\n添加到行尾來爲每條記錄添加換行符。

for row in cursor: 
    outputFile.write('{0}, {1}, {2}\n'.format(row[0], row[1][0], row[1][1]))