いくつかの地理空間的なpythonを学ぼうとしています。多かれ少なかれ、クラスノートhereに従ってください。以下の私に与え無効なPythonの構文file.writeを使用して
#!/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()
ランニング:Pythonの2.7.1
実行
File "shape_summary.py", line 38
file.write(id + ' ' + x + ' ' + y + ' ' + cover + '\n')
^
SyntaxError: invalid syntax
すべてのヘルプは素晴らしいだろう!また、単にスタイルのコメントを
y = str(geometry.GetY())
:
@eyquem - ファイルとIDの両方がPython標準ライブラリで広く再利用されています機能に組み込まれています。非常に便利で説明的な名前です。 'file'はPython 3では削除されていますが、一般的にはPython 2でも使われていないので、避ける理由はほとんどありません。加えて、OPは英語をはっきりと理解していますし、Stack Overflowの_linguaフランチャもありますので、誰もが理解できるようにコメントを英語で投稿してください。 Foo Bahのコメントをフランス語で再掲する理由は全くありませんでした。 – agf