buehlerd

Parsing and converting table to .txt files

Discussion created by buehlerd on Jun 18, 2014
Latest reply on Jul 2, 2015 by pschneider
The setup is we currently do not have an automated work order system in place.  IT has approached me to build an internet GIS based service request application, of which the local government model template "Citizen Service Request" works for what we want it to do.  On the back end I am scripting out how to update the features from unassigned to assigned after creating some .csv/.txt files to attach to emails sent via SQL Server Business Intelligence Development Studio.  I have put together the python code below as a proof of concept before switching out the necessary items for our SDE database. This will be a stand-alone script running on the server at a regular interval (if that matters? Suggestions?).

My questions are:
1) I get it to produce an intermediate table based on if the STATUS field has the value 'Unassigned'. I would like to take it a step further and have it parse out based on request type, and the unassigned status.  Example would be: potholes, street sign damage, and debris in road to be written to one table. Sidewalk damage, weeds, and graffiti to another, and so on till I get the attachments for the group of people I need to send them to.  Is there an elegant way of doing that or do I need to have multiple table to table conversions and expressions?

2) Right now it converts that one table to a text file that can be added as an attachment in the email.  I would like to take it to the next level, which is to convert the created tables from question 1.  I would assume I could just copy the code over and over with the appropriate tables to convert, is there a better way to do that?

3) Building on question 2, If there are no records I would like it to not clunk out because there are no records in the table to convert. I am pretty sure I need an if else or if then statement, but I am not proficient with python scripting.  Any help would appreciated.
#importing modules
import arcpy
import os
import csv

# set the workspace
arcpy.env.workspace = "F:/Home/GIS/Depts/GIS/proj/ServiceRequest/ServiceRequest.gdb"
arcpy.env.overwriteOutput = True

#setting variables
workspace = arcpy.env.workspace
fc = "/ServiceRequests"
fields = ["STATUS","REQUESTID"]
expression = arcpy.AddFieldDelimiters ("workspace", "STATUS") + " = 'Unassigned'"

print "Starting transport Captain"

#setting local variables and setting up Table to Table Conversion
inTable = "ServiceRequests"
outLocation = workspace
outTable = "Unassigned"

#Table to Table conversion

arcpy.TableToTable_conversion (inTable, outLocation, outTable, expression)

print "Table made it to destination Captain"

#updating records from unassigned to assigned
fcToUpdate = "F:/Home/GIS/Depts/GIS/proj/ServiceRequest/ServiceRequest.gdb/ServiceRequests"
affectedField = "STATUS"
oldValue =  'Unassigned'
newValue = 'Assigned'

queryString = '"' + affectedField + '" = ' + "'" + oldValue + "'"

with arcpy.da.UpdateCursor(fcToUpdate, (affectedField,), queryString) as cursor:
   for row in cursor:
      row[0] = newValue
      cursor.updateRow(row)


# Converting FileGeoDatabase Table to .TXT for emailing
print "Starting conversion GIS to CSV Captain"

table = outTable
outfile = "F:/Home/GIS/Depts/GIS/proj/ServiceRequest/Unassigned.txt"

#--first lets make a list of all of the fields in the table
fields = arcpy.ListFields(table)
field_names = [field.name for field in fields]

with open(outfile,'wb') as f:
    w = csv.writer(f)
    #--write all field names to the output file
    w.writerow(field_names)

    #--now we make the search cursor that will iterate through the rows of the table
    for row in arcpy.SearchCursor(table):
        field_vals = [row.getValue(field.name) for field in fields]
        w.writerow(field_vals)
    del row
    del table
    del outTable

print "Mission Success Captain. Returning to Base"

Outcomes