일부 지형 공간 파이썬을 배우려고합니다. 수업 노트 here에 다소 차이가 있습니다. 다음 나에게주는file.write를 사용하는 Python 구문이 잘못되었습니다.
#!/usr/bin/python
# import modules
import ogr, sys, os
# set working dir
os.chdir('/home/jacques/misc/pythongis/data')
# create the text file we're writing to
file = open('data_export.txt', 'w')
# import the required driver for .shp
driver = ogr.GetDriverByName('ESRI Shapefile')
# open the datasource
data = driver.Open('road_surveys.shp', 1)
if data is None:
print 'Error, could not locate file'
sys.exit(1)
# grab the datalayer
layer = data.GetLayer()
# loop through the features
feature = layer.GetNextFeature()
while feature:
# acquire attributes
id = feature.GetFieldAsString('Site_Id')
date = feature.GetFieldAsString('Date')
# get coordinates
geometry = feature.GetGeometryRef()
x = str(geometry.GetX())
y = str(geometry.GetY()
# write to the file
file.Write(id + ' ' + x + ' ' + y + ' ' + cover + '\n')
# remove the current feature, and get a new one
feature.Destroy()
feature = layer.GetNextFeature()
# close the data source
datasource.Destroy()
file.close()
달리기 : 파이썬 2.7.1
실행
File "shape_summary.py", line 38
file.write(id + ' ' + x + ' ' + y + ' ' + cover + '\n')
^
SyntaxError: invalid syntax
어떤 도움을 환상적 일 것입니다!
@eyquem - 파일과 ID 모두 Python 표준 라이브러리에서 광범위하게 재사용됩니다. 기능이 내장되어있어 매우 유용하고 설명적인 이름입니다. 'file'은 파이썬 3에서 제거되었고 파이썬 2에서도 일반적으로 사용되지 않았습니다. 그래서 그것을 피할 이유가 거의 없습니다. 또한 OP는 영어를 명확하게 이해하고 있으며 스택 오버플로의 _ 링거 프랑이기도하므로 누구나 이해할 수 있도록 영어로 의견을 게시하십시오. Foo Bah의 의견을 프랑스어로 다시 말할 이유는 없었습니다. – agf