Write records to csv

768
14
11-28-2011 04:23 AM
DaveJordan1
New Contributor III
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
JakeSkinner
Esri Esteemed Contributor
Hi Dave,

You can do this by iterating through each item in your list.  Add the following to your script:

import arcpy
from arcpy import env

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

rows = arcpy.SearchCursor(fc, "OBJECTID < 5")

fields = arcpy.ListFields(fc)

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))
            for item in record:
                   f.writelines("%s\n" % item)
            record = []

f.close()
del row, rows
0 Kudos
DaveJordan1
New Contributor III
Thank you for taking time to look at this.  Your suggestion does clean up the output, but it prints each field value in the record to a newline.  I need each value to be separated by a comma and each record to a newline...

example: 
24, 37,  22', 01, B, 7, 0, , , 0.59999999999999998
24, 37,  22', 01, B, 8, 0, , , 0.59999999999999998
24, 37,  22', 01, B, 9, 0, , , 0.59999999999999998
0 Kudos
JakeSkinner
Esri Esteemed Contributor
There may be an easier way, but I was able to accomplish this by writing each field to the output text file.  Ex:

import arcpy
from arcpy import env

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

rows = arcpy.SearchCursor(fc)

for row in rows:
            f.write(str(row.getValue("OBJECTID")) + ", " + str(row.getValue("LU_CODE")) + ", " + str(row.getValue("ZONE_CODE")) + "\n")

f.close()
del row, rows
0 Kudos
DaveJordan
New Contributor
Jake,

I've spent the better part of the day trying to get this to work.  I keep thinking that I must be missing something.  Its failing on the WRITE statement.  I've simplified it down to just a single field (OBJECTID) for testing.   Here is the error I am getting:

Running script Test...
Traceback (most recent call last):
  File "C:\Dev\ArcGIS10\Python\DaveTest.py", line 44, in <module>
    f.write(str(row.getValue("OBJECTID")) + "\n")
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\arcobjects.py", line 945, in getValue
    return convertArcObjectToPythonObject(self._arc_object.GetValue(*gp_fixargs(args)))
RuntimeError: ERROR 999999: Error executing function.
ERROR 999999: Error executing function.
Completed script Test...
Failed to execute (Test).
0 Kudos
MathewCoyle
Frequent Contributor
Hi Dave, I tested Jake's script and it works fine for me. Are you using proper syntax/indentation? Is there a field called "OBJECTID"? Do you have write access to that directory on Y-drive? Not sure what would be causing this issue.

Try something simple like
rows = arcpy.SearchCursor("parcels")
for row in rows:
    print row.getValue("OBJECTID")

Then add on line by line until you run into a wall.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Dave,

Feel free to post your entire block of code.  Be sure to wrap it in
 tags (using # symbol above) to preserve indentation.
0 Kudos
DaveJordan1
New Contributor III
Here is the entire code:


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 list of fields
  fields = arcpy.ListFields(fc, "", "")

  # Create a search cursor 
  rows = arcpy.SearchCursor(fc,"","","TOWNSHIP; RANGE; SEC; SUBCODE; BLOCK; LOT","") 


  for row in rows:
        f.write(str(row.getValue("OBJECTID")) + "\n")
# Previous attempts-
#      f.writelines(str(row.getValue("TOWNSHIP")) + ", " + "\n")
#  (row.getValue("TOWNSHIP") + ", " str(row.getValue("RANGE")) + ", " + str(row.getValue("SEC")) + ", " + str(row.getValue("SUBCODE")) + ", " + str(row.getValue("BLOCK")) + ", " + str(row.getValue("LOT")) + "\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
MathewCoyle
Frequent 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.
0 Kudos
DaveJordan1
New Contributor III
Hi Dave, I tested Jake's script and it works fine for me. Are you using proper syntax/indentation? Is there a field called "OBJECTID"? Do you have write access to that directory on Y-drive? Not sure what would be causing this issue.

Try something simple like
rows = arcpy.SearchCursor("parcels")
for row in rows:
    print row.getValue("OBJECTID")

Then add on line by line until you run into a wall.



I tried your sample also.  It gives the same 99999 useless error.  I believe I am using the proper indentation.  Yes there is a field called OBJECTID and yes I have write permission to the directory.

Extra info - I have kept it simple for development purposes, There is only one data frame and one feature class (parcels) in this mxd. 
I have successfully written out individual values from fields in the row using - For Field in Fields -

Thanks for helping with this.
0 Kudos