I am consuming a json web-service with NumPy Array to Feature, I have many nested try/except blocks to consume the data/find errors/etc. The script is functional, however I am having trouble seeing where I am obviously not accounting for a loop to consume the data.
Here is a crude example of my code structure; I am writing the first json object that I loop through to my db, although hundreds are returned. How can I adjust my code to account for all of my loops and incoming data?
import json
import jsonpickle
import requests
import arcpy
import numpy as np
import time
import datetime
import logging
import logging.handlers
import smtplib
#overall exception handling
class TlsSMTPHandler(logging.handlers.SMTPHandler):
def emit(self, record):
try:
import smtplib
import string # for tls add this line
except:
do something
logger = logging.getLogger()
#arcpy e-mail handling
try:
def sendResultEmail(msgContents, success_TF):
try:
if arcpy.Exists(fc):
arcpy.Delete_management(fc)
try:
requests stuff
except requests.exceptions.ConnectTimeout as e:
print "Too slow Mojo!"
if last_run != now_minus_5:
print "this block is activated"
try:
items = []
for sr in decoded2['Response']['ListOfServiceRequest']['ServiceRequest']:
SRAddress = sr['SRAddress']
# SRType = sr['SRType']
if sr['Latitude'] != '':
y = sr['Latitude']
if sr['Longitude'] != '':
x = sr['Longitude']
if sr['Latitude'] == '':
blanky = sr['Latitude']
if sr['Longitude'] == '':
blankx= sr['Longitude']
print blanky
print blankx
#many try except blocks that consume data
try:
locationEwastePrevious = ''
for sr in ElectronicWaste:
for ewastelocation in ElectronicWaste['La311ElectronicWaste']:
locationewaste = ewastelocation['CollectionLocation']
if locationEwastePrevious != locationewaste:
locationEwastePrevious = locationewaste
except:
print "No Electronic Waste Types"
try:
itemEwastePrevious = ''
for sr in ElectronicWaste:
for ewastetype in ElectronicWaste['La311ElectronicWaste']:
itemEwaste = ewastetype['ElectronicWestType']
if itemEwastePrevious != itemEwaste:
itemEwastePrevious = itemEwaste
except:
print"No Ewaste items"
try:
iteminfoewasteprevious = ''
countEwastePrevious = ''
for sr in ElectronicWaste:
for ewastecount in ElectronicWaste['La311ElectronicWaste']:
countEwaste = ewastecount['ItemCount']
if countEwastePrevious != countEwaste:
countEwastePrevious = countEwaste
iteminfoewaste = '{0}, {1}, '.format(countEwastePrevious, itemEwastePrevious)
if iteminfoewasteprevious != iteminfoewaste:
iteminfoewasteprevious = iteminfoewaste
ItemDesc = BulkyItemInfo + DACItemInfo
outputobjects= int(decoded2['Response']['NumOutputObjects'])
dt = np.dtype([('Address', 'U40'),
('LatitudeShape', '<f8'),
('LongitudeShape', '<f8'),
('Latitude', '<f8'),
('Longitude', '<f8'),
('ReasonCode','U128'),
('SRNumber', 'U40'),
('FirstName', 'U40'),
('LastName', 'U40'),
('ResolutionCode','U128'),
('HomePhone', 'U40'),
('CreatedDate', 'U128'),
('UpdatedDate', 'U128'),
('ItemDesc','U128' ),
])
items.append((SRAddress,
x,
y,
x,
y,
ReasonCode,
SRNumber,
FirstName,
LastName,
ResolutionCode,
HomePhone,
CreatedDate,
UpdatedDate,
ItemDesc,
))
sr = arcpy.SpatialReference(4326)
arr = np.array(items,dtype=dt)
NumPyArray = arcpy.da.NumPyArrayToFeatureClass(arr, fc, ['longitudeshape', 'latitudeshape'], sr)
except:
print "this does not work"
except arcpy.ExecuteError:
strResult += arcpy.GetMessages()
arcpy.AddMessage(strResult)
Output: I am expecting 187 different types for this request. This output writes correctly to my FC.
[ (u'4437 1/2 W LOCKWOOD AVE, 90029', -118.290563, 34.08883, -118.290563, 34.08883, u'44', u'1-7868751', u'', u'', u'', u'', u'2015-03-23 14:35:39', u'2015-05-26 06:39:15', u' 1, Bird Cage (Plastic) ')]
items
appears to be getting appended to only once (it's not within any of your for loops as far as I can see). Could possibly be an indentation issue, as there is a confusing mix of indentation here. – DWynne May 26 at 17:30items
is appeneded to for the count of my returned records? – Geoffrey West May 26 at 17:53items.append
is supposed to be within a for loop, but it isn't right now. – DWynne May 26 at 17:55