通过ArcPy将表导出到XYZ ASCII文件?


23

我正在寻找一种通过ArcPy 将ArcGIS表(使用Sample工具创建的)导出到文本文件的方法。

我可以通过右键单击表通过上下文菜单在ArcGIS中执行此操作,但是还没有找到编写脚本的方法。

Answers:


31

您可以使用光标从表中获取数据并将其写入逗号分隔的文本文件中。

编辑:我正在添加一个更简洁的代码块,以使用csvPython模块完成任务

使用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()

很高兴能为您提供帮助@Toni
Jason

1
@Jason-谢谢,这非常有帮助。我是新来的,所以我不敢评论您接受的答案。我认为使用arcpy.da游标的新答案中存在一个小错误。with arcpy.da.SearchCursor(table) as cursor:应该是with arcpy.da.SearchCursor(table, field_names) as cursor:

@TylerG很好,我已经编辑了答案,以包括数据访问游标所需的字段列表。谢谢。
杰森

8

您可能需要“将要素属性导出为ASCII”,巧妙地命名为arcpy.ExportXYv_stats

http://help.arcgis.com/zh-CN/arcgisdesktop/10.0/help/index.html#//005p0000003v000000

import 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侦探!由于必须指定字段名称,因此它可以交互方式工作,但在模型或脚本中效果不佳。
马特·威尔基

1

这是我使用的一段代码。它可以帮助我将所有输出文件生成为.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")
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.