I'm a programing noob, and I'm stuck... My goal is to open a csv file (an export from program A), reformat the data, and write it to a new csv file (for program B to import). I know my code isn't pretty, though it works up until it writes to the new csv file. It only writes the last row of data from the old csv.
import csv
print 'Enter file location or press enter for default location.'
aPath = raw_input('> ') # user path to file
if aPath == '':
aPath = '/default/path/to/file' # default path
aFile = raw_input('Enter file name: ') # user file name
if aFile == '':
aFile = 'orginal.csv' # default file name
myCSV = csv.DictReader(open(aPath + aFile, 'r'), delimiter=',')
print myCSV.fieldnames # verify fieldnames
for p in myCSV:
w = csv.writer(open("output.csv", "w"))
try:
p = dict((k, v) for k, v in p.iteritems()
if v.lower() != 'null')
except AttributeError, e:
print e
print p
raise Exception()
# reformats the columns of data for the output.csv
pID = p.get('Last Name')[-4:]
pName = p.get('Last Name')[:-4].strip() + ', ' + p.get('First Name')
pDate = p.get('Start Time')[:-5]
pBlank = p.get('')
pCourse = p.get('Assigned Through')
pScore = p.get('Score')[:-1]
# verifies the new columns
print pID, pName, pDate, pBlank, pCourse, pBlank, pBlank, pScore
# NOT working...Only writes the last row of the orginal.csv to output.csv
w.writerow([pID, pName, pDate, pBlank, pCourse, pBlank, pBlank, pScore])