taichi108

How do I create and write to a new text or excel file?

Discussion created by taichi108 on Jan 31, 2011
Latest reply on Feb 2, 2011 by taichi108
I've written a python script that loops thru all records in a shapefile, checks each record for certain criteria and prints out certain fields from the records that meet the condition. That is working fine. Now instead of printing the results, I want to write these certain fields to either a text file or an excel file. Any suggestions on what commands or simple block of code might accompliss this?

Here is my script that prints my results:

import arcpy

from arcpy import env
env.workspace = "C:/workspace"
pin_hold = "999999999999"
area_hold = 0
township_hold = ""
section_hold = ""
quarter_hold = ""
parcelname_hold = ""
public_hold = ""
corrsource_hold = ""

text_file = open("rename_pins.txt", "w")
rows = arcpy.SearchCursor("parcels_Identity6.SHP", "", "", "", "PIN; PARCELNAME; BLMPIN; TRANSFER; PUBLIC_; PARCEL_KEY; CORR_SOURC; Shape_area; Shape_len; Id; Township; Section; Quarter", "PIN A")

for row in rows:
    if row.PIN == pin_hold:
        if row.Shape_area > area_hold:
            area_hold = row.Shape_area
            township_hold = row.Township
            section_hold = row.Section
            quarter_hold = row.Quarter
            parcelname_hold = row.PARCELNAME
            public_hold = row.PUBLIC_
            corrsource_hold = row.CORR_SOURC
    else:
        if pin_hold[0:4] != township_hold or pin_hold[4:6] != section_hold or pin_hold[6] != quarter_hold:
            print pin_hold + ' ' + str(area_hold) + ' ' + parcelname_hold + ' ' + public_hold + ' ' + corrsource_hold
        pin_hold = row.PIN
        area_hold = row.Shape_area
        township_hold = row.Township
        section_hold = row.Section
        quarter_hold = row.Quarter
        parcelname_hold = row.PARCELNAME
        public_hold = row.PUBLIC_
        corrsource_hold = row.CORR_SOURC

print 'completed'

Thanks,
Gary

Outcomes