Export feature layer data to text file

196
3
Jump to solution
2 weeks ago
LindseyStone
Occasional Contributor III

Let me start with I'm very new to Python so please bare with me.  I have created a python script that deletes a feature layer "ElectricMeterDuplicates" out of a GDB, runs some other calculations commands and then exports the results back into the original GDB as a feature layer with the same name.  That part is all working correctly until I get to my text file I want to write results to.  I could have one of two situations. 1. the new feature layer contains 2 or more rows of data, here I want it to write to the text file the information form 2 fields for each row of data. OR the feature layer is empty and here I want it to write to the file I simple message of no data . 

I have written the below script.  When I run it in Notebook within Pro it correctly writes to the text file if there is data, however, if there is no data it doesn't write anything.  When I run this as a standalone script through a bat file (which is my end goal, the entire thing errors out with ERROR 000732: Input Rows: Dataset ElectricMeterDuplicates does not exist or is not supported and doesn't write anything to the text file no matter the situation.

epath = r"Z:\General\Projects\Utility\Scripts\DuplicateMeters\Logs\ElectricDupMeters.txt"
efile = open(epath, 'a')
etable = r"Z:\General\Projects\Utility\Scripts\DuplicateMeters\DuplicateMeters.gdb\ElectricMeterDuplicates"
fields = ['meternum','assetid']
today = str(date.today())
total = 0

row = arcpy.management.GetCount(
    in_rows="ElectricMeterDuplicates"
)
print (row)
if row == 0:
    print ("Empty List")
    efile.write("No Duplicate Meters on " + today + "\n")
else:
     with arcpy.da.SearchCursor(etable,fields) as cursor:
        for row in cursor:
            print ("Meter {} is duplicated on Asset ID {}".format(row[0],row[1]))
            efile.write("Recorded On " + today + " Meter {} is duplicated on Asset ID {}".format(row[0],row[1]) + "\n")
        
efile.close()

 

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
LindseyStone
Occasional Contributor III

Found my issue.  It ended up being two things.  One I was calling out the row in two different areas and the count field was not processing correctly in my if statement was I turned the count into a string then it found the 0 values.  Here is my final working one.

epath = Elog2
efile = open(epath, 'a')
etable = ElectricDup
fields = ['meternum','assetid']
today = str(date.today())
total = 0

count_row = str(arcpy.management.GetCount(
    in_rows = etable)
)
print (count_row)
if count_row == '0':
    print ("Empty List")
    efile.write("No Duplicate Meters on " + today + "\n")
else:
     with arcpy.da.SearchCursor(etable,fields) as cursor:
        for row in cursor:
            print ("Meter {} is duplicated on Asset ID {}".format(row[0],row[1]))
            efile.write("Recorded On " + today + " Meter {} is duplicated on Asset ID {}".format(row[0],row[1]) + "\n")
        
efile.close()

View solution in original post

0 Kudos
3 Replies
DanPatterson
MVP Esteemed Contributor
# in_rows="ElectricMeterDuplicates"
# try
in_rows = etable

... sort of retired...
LindseyStone
Occasional Contributor III

Found my issue.  It ended up being two things.  One I was calling out the row in two different areas and the count field was not processing correctly in my if statement was I turned the count into a string then it found the 0 values.  Here is my final working one.

epath = Elog2
efile = open(epath, 'a')
etable = ElectricDup
fields = ['meternum','assetid']
today = str(date.today())
total = 0

count_row = str(arcpy.management.GetCount(
    in_rows = etable)
)
print (count_row)
if count_row == '0':
    print ("Empty List")
    efile.write("No Duplicate Meters on " + today + "\n")
else:
     with arcpy.da.SearchCursor(etable,fields) as cursor:
        for row in cursor:
            print ("Meter {} is duplicated on Asset ID {}".format(row[0],row[1]))
            efile.write("Recorded On " + today + " Meter {} is duplicated on Asset ID {}".format(row[0],row[1]) + "\n")
        
efile.close()
0 Kudos
DanPatterson
MVP Esteemed Contributor

plus the etable thing in my thread 😉


... sort of retired...
0 Kudos