Write records to csv

1399
14
11-28-2011 04:23 AM
DaveJordan1
Regular Contributor
My ultimate goal is to create mailing labels from selected parcel polygons. using the following code I am attempting to write out to a txt file the selected records. It works but I am having issues with the output it is giving me.
1) before each field that contains a string value it is inserting the letter "u".
2) I want to remove the quotes from each field and the brackets.
3) If there is a better way to do this, please let me know

Here is the code followed by a sample of the output
-----------------------------------------------------
import arcpy
from arcpy import env
try:
mxd = arcpy.mapping.MapDocument("Current")
df = arcpy.mapping.ListDataFrames(mxd, "ParcelDF")[0]
Parcels = "parcels"
fc = arcpy.mapping.ListLayers(mxd, Parcels, df)[0]
f = open("Y:\Notification Radius Pkgs\Setup10\Test.txt", 'w')

# Create a search cursor
rows = arcpy.SearchCursor(fc)

# Create a list of fields
fields = arcpy.ListFields(fc, "", "")

# Write the output to a text file
record = [""]
for row in rows:
for field in fields:
if field.type != "Geometry":
#print "%s: Value = %s" % (field.name, row.getValue(field.name))
record.append(row.getValue(field.name))
f.writelines("%s\n" % record)
record = [""]

f.close-------------------------------------------------------------------------
SAMPLE OUTPUT
[' ', 27521, u'24', u'37', u'22', u'01', u'B', u'7', 0, u' ', u' ', 0.59999999999999998]
Tags (2)
0 Kudos
14 Replies
MathewCoyle
Honored Contributor
If you are failing on this,
rows = arcpy.SearchCursor("parcels")
for row in rows:
    print row.getValue("OBJECTID")

and "parcels" is a valid feature layer with OBJECTID as a field, you have some other environment problems outside of your code. Try rebooting your computer and running it again.
0 Kudos
DaveJordan1
Regular Contributor
Oh, your field limiters are taking out OBJECTID...
rows = arcpy.SearchCursor(fc,"","","TOWNSHIP; RANGE; SEC; SUBCODE; BLOCK; LOT","") 


Oh sorry, after testing even that should work, I guess OBJECTID is protected from field limiters. Not sure what your issue is, works fine even with the non raw path and improper indentation.


Yes, I added OBJECTID in and it still fails.  I think my next step is to start all over with a clean script... 
I 've tried so many different approaches...  Is'nt the measure of insanity something like, "One's tendancy to perform the same act over and over again, hoping for different results, regardless of the fact that the results are constant"...or something like that!
0 Kudos
MathewCoyle
Honored Contributor
That's Einstein's definition yes.

Try to copy and paste this in the ArcMap python window after a reboot.

import arcpy
from arcpy import env
try:
    mxd = arcpy.mapping.MapDocument("Current")
    df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
    Parcels = "parcels"
    fc = arcpy.mapping.ListLayers(mxd, Parcels, df)[0]
    f = open(r"Y:\Notification Radius Pkgs\Setup10\Test\Test.txt", 'w')

    # Create a search cursor
    rows = arcpy.SearchCursor(fc)
    for row in rows:
        f.write(str(row.getValue("OBJECTID")) + "\n")

    f.close()
    del row, rows

except Exception, e:
    import traceback
    f.close
    map(arcpy.AddError, traceback.format_exc().split("\n"))
    arcpy.AddError(str(e))
0 Kudos
DaveJordan1
Regular Contributor
I figured out the cause, or at least I got past it.  The Parcels layer I was working with was Joined to another table.  Once I removed the join, it ran as expected.  Even though I wasn't referencing any joined fields...
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Dave,

When a join exists, the field name will change to 'feature class/table name.field name'.  So when calling the fields to write to the text file you would need to specify the parcel feature class name.  Ex:

f.write(str(row.getValue("parcels.OBJECTID")) + ", " + str(row.getValue("parcels.LU_CODE")) + ", " + str(row.getValue("parcels.ZONE_CODE")) + "\n")
0 Kudos