I got a good lesson this week in why it’s necessary to think out the code, before you start coding. I’ve been working on learning Python as my general scripting language. Frankly I find it easier to understand than Perl. Anyway….I wrote a Python script to strip out a list of records from a mixed asset file. I thought to my self, it couldn’t really take that long. That’s what I thought…..
It end up taking several hours, and interruptions, to finish. What’s sad is, you would think it’s a complex script….but it isn’t. It’s fairly simple and probably would have taken anyone else minutes to crank it out. Ah well….it was still fun…
So as I sit down to read/prep for my assignments this week…I see that the assignment is to generate structure code with UML. Suddenly, that task doesn’t seem as boring and useless as it used to last week. Especially given the size of the Gradebook application we are creating.
The Python script that whooped my butt
# fixed assets remove retired
# this program removes the retired assets from the master list
import csv
# open files
masterReader = open('e:\\master.csv', 'r')
retireReader = open('e:\\retire.csv','r')
csvout = open("e:\\CSVOut.csv", "w")
# create readers and the writer
readMaster = csv.reader(masterReader)
readRetire = csv.reader(retireReader)
writer = csv.writer(csvout, dialect='excel')
retireList = []
# main
###### read retire asset number into a list
for asset in readRetire:
retireList.append(asset[0])
#### read row from master file, compare asset number to retirement list
#### created above. if not in retirement list, write to output file
for row in readMaster:
if row[0] not in retireList:
writer.writerow(row)
# close open files
masterReader.close()
retireReader.close()
csvout.close()
Good Times I tell ya.
