0

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

2 Answers2

1

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 missingFromAuthoritative list 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 with block:

with open('missing.csv', "wb") as outFile:
    outCsv = csv.writer(outFile)
    outCsv.write(header)
    for missing in missingFromAuthoritative:
        # do your other stuff with inconsistencies, etc., here
        print missing
        outCsv.writerow(missing)
BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • That worked, thanks. I was thinking it had to be something like that just couldn't get it. Also I had to put at the start to stop a syntax error. – user1614303 Aug 22 '12 at 14:21
0

Your writing is consuming the iterator. You need to rearchitect your code.

Pseudocode:

Perform query
Open CSV file
Write header to CSV file
For each row in the query:
  Display row to screen
  Write row to CSV file
Close CSV file
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358