So I have a list that gets generated e.g. missingfromauthoratative and with that list I want to do two things.
1. Print the results on the screen and;
2. Write the results to a csv file.
With what I have it will do one or the other or sometimes a combination. The code below is what I have.
#Find what is missing or new
missingFromAuthoritative = dBase.execute('SELECT url, mapindex, mapname, layerid, layername FROM authoritative EXCEPT SELECT url, mapindex, mapname, layerid, layername FROM current;')
newToAuthoritative = dBase.execute('SELECT url, mapindex, mapname, layerid, layername FROM current EXCEPT SELECT url, mapindex, mapname, layerid, layername FROM authoritative;')
#Print missing services (-) and new services (+)
for missing in missingFromAuthoritative:
print '-', missing[0], '\n Map: ', missing[2], '\n Layer:', missing[4], '\n'
self.inconsistency += 1
outcsv = csv.writer(open('missing.csv', "wb"))
header = ("Map Service","Map Index","Data Frame","Layer ID","Layer Name")
outcsv.writerow(header)
outcsv.writerows(missingFromAuthoritative)
It also sometimes writes one row to the screen and the other three to the csv.
This is what I want on the screen
– MapServer
Map: Layers
Layer: Proposed Power Generation Sites
– MapServer
Map: Layers
Layer: Existing Infrastructure
– MapServer
Map: Layers
Layer: Existing Utility Sites
– MapServer
Map: Layers
Layer: Existing Transmission Lines
4 inconsistencies found in 81.28 seconds.
and this is what I want in the csv
Map Service Map Index Data Frame Layer ID Layer Name
MapServer 0 Layers 0 Proposed Power Generation Sites
MapServer 0 Layers 1 Existing Infrastructure
MapServer 0 Layers 2 Existing Utility Sites
MapServer 0 Layers 3 Existing Transmission Lines
I have no idea why putting them after the for iterator doesn’t work. The only way to get wither to work is to comment the other out.
Thanks
You have several problems. You are reopening the file on every loop iteration, and thus truncating it every time. You are also writing the header and the entire
missingFromAuthoritativelist on every iteration, masking the first bug. Also, every time you iterate through the list, you are consuming a row from the query, so by the time you get to the end, there is nothing left to write.You should open the file once before the loop, write the header before the loop, write one row to the csv on each iteration, and close the file afterwards. You can do this with a
withblock: