Error exporting feature attributes

3262
13
10-24-2012 10:39 AM
steveSuperczynski
New Contributor
Has anyone else experienced any problems using the Export Feature Attributes to ASCII tool via python window?

I am using the following command yet it keeps telling me there is a syntax error, even though I followed the help on the tool syntax:

arcpy.ExportXYv_stats(infile,"State_Code";"County_Code";"Site_Code";"RASTERVALU","COMMA",outfile,"ADD_FIELD_NAMES")

-Steve
0 Kudos
13 Replies
T__WayneWhitley
Frequent Contributor
The problem is the tool 'sees' that you've provided more than the 5 parameters allowed.

This is the syntax:
ExportXYv_stats (Input_Feature_Class, Value_Field, Delimiter, Output_ASCII_File, Add_Field_Names_to_Output)

Your Value_Field is expecting a Python list, so test this:
arcpy.ExportXYv_stats(infile,["State_Code","County_Code","Site_Code","RASTERVALU"],"COMMA",outfile,"ADD_FIELD_NAMES")


Caleb Mackey made a great post (see link below), using the technique to populate his fieldnames list from another object list return on the ListFields method (see the red highlighted variables and corresponding brackets delineating the list):
http://forums.arcgis.com/threads/66118-Problem-with-coding-on-ExportXYv_stats-tool?highlight=Exportx...

fieldnames = [f.name for f in arcpy.ListFields(outfeat)]
arcpy.ExportXYv_stats(outfeat, fieldnames, "COMMA", out_table,"ADD_FIELD_NAMES")

I've used this tool before but have forgotten the syntax...don't think I've fed in my fields like that before though.
Thanks, I liked it so much I had to give Caleb points for that!...well, 1 point anyway. If that works for you, you could give him another point if you like.
0 Kudos
by Anonymous User
Not applicable
Wow, this is funny!  I was going to post a reply on how I use the exportXYv_stats tool and I am seeing a reference to one of my previous posts showing how I do it!  Thanks Wayne for the point!  I wanted to post some code on here of something I was playing around with yesterday, which is an alternative to using the exportXYv_stats tool.  I have found that this actually works much faster than creating a csv file with the exportXYv_stats tool.  This method uses a search cursor to grab values and write them out into a new csv file.  I tested this and it worked on 9,000 Address points in 3 seconds! Very Cool!  Anyways here is an another way to write out attributes to text.

    print 'Creating Text File...'
    text = open(p.join(ws, "Addresses2.csv"), "w")
    text.writelines('"PID","FULL_ADD","POINT_X","POINT_Y"')
    srows = arcpy.SearchCursor(address)
    for row in srows:
        pid = row.getValue('PID')
        add = row.getValue('FULL_ADD')
        x   = row.getValue('POINT_X')
        y   = row.getValue('POINT_Y')
        text.writelines('\n"%s","%s",%s,%s' % (pid,add,x,y))
    text.close()
 
    del row, srows
    print 'Text File Created Successfully'


If you would rather make a tab delimited text file you can simply change the commas to '\t'.  This is obviously more ideal if you do not have ton of fields you want to export out or if you just want to quickly make stand alone tables using just a few fields from another table.  I also realize this is a lot more code than just using Esri's XYv_stats tool, but you have a lot more control over this.

One thing you need to pay attention to if you want to use this method is to wrap double quotes around the parameter substitutions for string fields ("%s")  and do not put double quotes around numeric fields (%s, x and y fields in my example).
0 Kudos
T__WayneWhitley
Frequent Contributor
Nice, rack 1 more point... I was going to write that I had modified the exportXYv_stats (but wasn't directly relevant to this post at the time) in order not to trip on Unicode characters - don't know if Unicode is supported now, and it has been awhile since I checked it out.  I don't know, maybe I should have stripped out the Unicode to replace ANSI text in the fc I was dealing with (which was doable) but it isn't a source fc I am responsible...so guess I got lazy and it was easier to accommodate!  🙂

Anyway, thanks Caleb!
0 Kudos
by Anonymous User
Not applicable
I suppose I was getting a little off topic here, but I just thought I would provide another way of writing out text files that is not using that tool if anyone is interested.

@ Steve- I should've mentioned in my first reply that Wayne was correct in that your error was being created because your field names were not in brackets.  Python did not know how to interpret your field list.  If you use his advice and wrap your field names in brackets, the tool should work fine. 

@ Wayne-  Hmm, you bring up a good point and I am now curious about writing unicoded values out to new text files. I have not played around with this yet so I do not know how that would work.  :confused:
0 Kudos
steveSuperczynski
New Contributor
Thanks Wayne and Caleb. The brackets solution fixed the error I was getting. I should have figured that the brackets were needed, I missed that in the help window.

Unfortunately this uncovered another error which only occurs when either running the tool in batch mode or through Python:
"ERROR 000204: Error creating input feature cursor"

I seem to remember coming across this error previously when searching for answers to my problem so I will have to go back and look for it.

Thanks!
0 Kudos
by Anonymous User
Not applicable
Can you post your full code?
0 Kudos
steveSuperczynski
New Contributor
Here is the code.

[PHP]import os, sys, glob, re
import arcpy

source = 'C:/DISCOVER_AQ/MODEL/CMAQ/PM2.5/'

for infile in glob.iglob(os.path.join(source, 'PM*.shp')):
    fn = os.path.splitext(os.path.basename(infile))[0]
    outfile = source+fn+'.txt'
    arcpy.ExportXYv_stats(infile,["State_Code","County_Code","Site_Code","RASTERVALU"],"COMMA",outfile,"ADD_FIELD_NAMES")
   
[/PHP]

-Steve
0 Kudos
by Anonymous User
Not applicable
Steve,

I do not see any problems with your code, but I have had lots of strange behavior with this tool which is why I decided to create an alternate way to write to text files.  I looked up the error message and found a link to this forum on GIS stack exchange:

http://gis.stackexchange.com/questions/23776/error-000204-using-export-feature-attribute-to-ascii-to...

According to this, the person changed the delimiter from comma to semi colon and it worked for some strange reason?  It may be worth a shot, but who knows.  There does not appear to be anything wrong with your script so I don't know why you are getting that error (unless I am overlooking something in your glob functions, I have never used that module before).

also, you can save yourself some time by importing os.path as p (from os import path as p).  This way instead of typing out "os.path.join" every time, you can replace it with "p.join".

hope this helps
0 Kudos
steveSuperczynski
New Contributor
Changing the delimiter didn't make any difference for me. Thanks for the tip though. I guess I will go your route and write my own script to export the attributes. I used this tool with similar files using batch mode before and had no issues so this is a bit puzzling.

Thanks for the os.path tip as well!

-Steve
0 Kudos