我可以通过右键单击表通过上下文菜单在ArcGIS中执行此操作,但是还没有找到编写脚本的方法。
#1 楼
您可以使用游标从表中获取数据并将其写入以逗号分隔的文本文件中。编辑:我添加了一个更简洁的代码块,以使用Python的
csv
模块使用arcpy.da游标的新答案:
import arcpy,csv
table =r'c:\path\to\table'
outfile = r'c:\path\to\output\ascii\text\file'
#--first lets make a list of all of the fields in the table
fields = arcpy.ListFields(table)
field_names = [field.name for field in fields]
with open(outfile,'wb') as f:
dw = csv.DictWriter(f,field_names)
#--write all field names to the output file
dw.writeheader()
#--now we make the search cursor that will iterate through the rows of the table
with arcpy.da.SearchCursor(table,field_names) as cursor:
for row in cursor:
dw.writerow(dict(zip(field_names,row)))
使用旧式游标的新答案:
import arcpy,csv
table =r'c:\path\to\table'
outfile = r'c:\path\to\output\ascii\text\file'
#--first lets make a list of all of the fields in the table
fields = arcpy.ListFields(table)
field_names = [field.name for field in fields]
with open(outfile,'wb') as f:
w = csv.writer(f)
#--write all field names to the output file
w.writerow(field_names)
#--now we make the search cursor that will iterate through the rows of the table
for row in arcpy.SearchCursor(table):
field_vals = [row.getValue(field.name) for field in fields]
w.writerow(field_vals)
del row
旧答案:
import arcpy
table =r'c:\path\to\table'
outfile = r'c:\path\to\output\ascii\text\file'
#--first lets make a list of all of the fields in the table
fields = arcpy.ListFields(table)
i = 1
f = open(outfile,'w')
for field in fields:
#--write all field names to the output file
if i < len(fields):
f.write('%s,' % field.name)
i += 1
else:
f.write('%s\n' % field.name)
#--now we make the search cursor that will iterate through the rows of the table
rows = arcpy.SearchCursor(table)
for row in rows:
i = 1
for field in fields:
if i < len(fields):
f.write('%s,' % row.getValue(field.name))
i += 1
else:
f.write('%s\n' % row.getValue(field.name))
del rows
f.close()
#2 楼
您可能需要巧妙地命名为arcpy.ExportXYv_stats的“将要素属性导出为ASCII” http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//005p0000003v000000import arcpy
feature = "path to feature here"
# fieldnames must be explicitly provided. Note that you will get additional fields based on the feature type (e.g., "XCoord" and "YCoord" for point features)
fieldnames = [X.name for X in arcpy.ListFields(feature)]
# delimiter options "SPACE", "COMMA", or "SEMI-COLON"
# header options "ADD_FIELD_NAMES" or "NO_FIELD_NAMES"
arcpy.ExportXYv_stats(feature, fieldnames, "SPACE", "path to outfile", "ADD_FIELD_NAMES")
评论
+1侦探!由于必须指定字段名称,因此它可以交互方式工作,但在模型或脚本中效果不佳。
–马特·威尔基
2014年5月27日19:46
#3 楼
这是我使用的一段代码。它可以帮助我将所有输出文件生成为.txt文件,范围从0,100。希望对您有所帮助for x in xrange(0,100):
if os.path.isfile(outfolder + "/" + "outputs" + str(x) +".shp" ):
inFeatures = "selected_features" + str(x) +".shp"
export_ASCII = "ASCII " + str(x) +".txt"
arcpy.ExportXYv_stats(inFeatures, ["Cur1_pr2","Cur3_pl1","slp1"],"SPACE", export_ASCII,"ADD_FIELD_NAMES")
评论
很高兴我可以帮助您@Toni
–杰森·贝利诺(Jason Bellino)
2011-12-14 19:50
@Jason-谢谢,这非常有帮助。我是新手,所以我不敢评论您接受的答案。我认为使用arcpy.da游标的新答案中存在一个小错误。以arcpy.da.SearchCursor(table)作为光标:应该与arcpy.da.SearchCursor(table,field_names)作为光标:
–user29069
2014年4月10日在18:58
@TylerG很好,我已经编辑了答案,以包括数据访问游标所需的字段列表。谢谢。
–杰森·贝利诺(Jason Bellino)
2014年4月11日,12:57