Eksportujesz tabelę do pliku XYZ ASCII przez ArcPy?


23

Szukam sposobu na wyeksportowanie tabeli ArcGIS (utworzonej za pomocą narzędzia Sample ) do pliku tekstowego za pomocą ArcPy.

Mogę to zrobić w ArcGIS za pomocą menu kontekstowego, klikając tabelę prawym przyciskiem myszy, ale nie znalazłem sposobu, aby to zrobić.

Odpowiedzi:


31

Możesz to zrobić za pomocą kursora, aby pobrać dane ze tabeli i zapisać w pliku tekstowym rozdzielanym przecinkami.

EDYCJA: Dodam bardziej zwięzły blok kodu, aby wykonać zadanie za pomocą csvmodułu Pythona

Nowa odpowiedź za pomocą kursora 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)))

Nowa odpowiedź za pomocą kursora w starym stylu:

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

Stara odpowiedź:

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

Cieszę się, że mogłem Ci pomóc @Toni
Jason

1
@Jason - Dzięki, to było bardzo pomocne. Jestem nowy, więc nie mam reputacji, by komentować zaakceptowaną odpowiedź. Myślę, że w nowej odpowiedzi, która używa kursora arcpy.da, jest mały błąd. with arcpy.da.SearchCursor(table) as cursor:powinno byćwith arcpy.da.SearchCursor(table, field_names) as cursor:

Dobry catch @TylerG, zredagowałem odpowiedź, aby uwzględnić listę pól wymaganych przez kursor Dostęp do danych. Dzięki.
Jason

8

Możesz chcieć „Eksportuj atrybut funkcji do ASCII” o sprytnej nazwie arcpy.ExportXYv_stats

http://help.arcgis.com/en/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 za wymyślenie! Działa to interaktywnie, ale nie tak dobrze w modelu lub skrypcie, ponieważ należy podać nazwy pól.
matt wilkie

1

Oto fragment kodu, którego używam. Pomaga mi generować wszystkie moje pliki wyjściowe do pliku .txt w zakresie od 0,100. Mam nadzieję, że to pomaga

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")
Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.