Select to view content in your preferred language

Export Feature Attribute to ASCII question

1657
6
04-09-2012 09:25 AM
DC
by
Emerging Contributor
Does anyone know a way to export every "Value_Field" in a shape file without specifically using the name of every one individually? In other words is there some kind of wildcard value or loop that will just export every single "Value_Field" that is in the shape file?

Based on this:
ExportXYv_stats (Input_Feature_Class, Value_Field, Delimiter, Output_ASCII_File, Add_Field_Names_to_Output)
Tags (2)
0 Kudos
6 Replies
MathewCoyle
Honored Contributor
I believe you are looking for something like this.
import arcpy
fields = arcpy.ListFields(<your_layer>)
field_text = list()
for f in fields:
    if fields.type != "Geometry":
        field_text.append(f.name)

arcpy.ExportXYv_stats(<your_layer>,field_text,"COMMA","C:/GIS/Exports/test2.asc","ADD_FIELD_NAMES")
0 Kudos
DC
by
Emerging Contributor
Thank you for the reply, but I am really new to python and not sure how that would fit into this:

# Export feature locations and attributes to an ASCII text file

# Import system modules

import arcpy


# Local variables...

workspace = "C:\Users\me\Desktop\InputFolder.ospfds"

input_features = "ShapeFile.shp"

export_ASCII = "ExportFile.txt"

 

try:

    # Set the current workspace (to avoid having to specify the full path to the feature classes each time)

    arcpy.env.workspace = workspace

 

    # Process: Export Feature Attribute to ASCII...

    arcpy.ExportXYv_stats(input_features, ["value_field_A","value_field_B","value_field_C"], "COMMA", export_ASCII, "ADD_FIELD_NAMES")

 

except:

    # If an error occurred when running the tool, print out the error message.

    print arcpy.GetMessages()
0 Kudos
DC
by
Emerging Contributor
Anyone else have any ideas on this?
0 Kudos
ScottBlankenbeckler
Deactivated User
Anyone else have any ideas on this?


I would use mathew's solution. Your code should look something like this:

# Export feature locations and attributes to an ASCII text file
 
# Import system modules
 
import arcpy
 
 
# Local variables...
 
workspace = "C:\Users\me\Desktop\InputFolder.ospfds"
 
input_features = "ShapeFile.shp"
fields = arcpy.ListFields (input_features )
field_text = list()
for f in fields:
    if fields.type != "Geometry":
        field_text.append(f.name)
 
export_ASCII = "ExportFile.txt"
 
 
 
try:
 
    # Set the current workspace (to avoid having to specify the full path to the feature classes each time)
 
    arcpy.env.workspace = workspace
 
 
 
    # Process: Export Feature Attribute to ASCII...
 
    arcpy.ExportXYv_stats(input_features, field_text, "COMMA", export_ASCII, "ADD_FIELD_NAMES")
 
 
 
except:
 
    # If an error occurred when running the tool, print out the error message.
 
    print arcpy.GetMessages()
 
0 Kudos
DC
by
Emerging Contributor
Ah, I got it to work with a couple changes.  Thank you both for the help!

1.  input_features = "ShapeFile.shp" needed the entire path instead of just the file name in order to work with arcpy.ListFields for some reason

2. "if fields.type != ...." needed to be changed to "if f.type != ...."
0 Kudos
DonBarker
Regular Contributor
Really appreciate you guys taking time to document this.  Am a Python novice, too.  Made a big contribution to my project.
0 Kudos