In Python 2.7 - How To Read Data From Csv, Reformat Data, And Write To New Csv
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 k
Solution 1:
You are re-opening the file and overwriting it on each pass through the for p in myCSV
loop. You need to create w
once before entering the for loop.
Solution 2:
The following modified code should work:
import csv
print'Enter file location or press enter for default location.'
aPath = raw_input('> ') # user path to fileif aPath == '':
aPath = '/default/path/to/file'# default path
aFile = raw_input('Enter file name: ') # user file nameif aFile == '':
aFile = 'orginal.csv'# default file name
myCSV = csv.DictReader(open(aPath + aFile, 'r'), delimiter=',')
print myCSV.fieldnames # verify fieldnameswithopen("output.csv", "wb") as ofile:
w = csv.writer(ofile)
for p in myCSV:
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 columnsprint 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])
Things we changed:
- We used the
with
operator. When working with opening and closing files, usingwith
is a generally accepted protocol to follow unless otherwise inapplicable since it handles the closing of files properly, among other things. - Use
wb
instead of justw
as your second argument toopen
. This will allow you to open the file in write-binary mode. See this for reference.
Hope this helps.
Solution 3:
I'm afraid you're re-writing your output file at each iteration ... You should simply take the line
w = csv.writer(open("output.csv", "w"))
out of the loop as :
w = csv.writer(open("output.csv", "w"))
try:
for p in myCSV:
try:
...
w.writerow([pID, pName, pDate, pBlank, pCourse, pBlank, pBlank, pScore])
finally:
w.close()
You could also have a look to "with" constructs that automatically cope with potential io errors and close the file at end of block with no need for the additional try.
Post a Comment for "In Python 2.7 - How To Read Data From Csv, Reformat Data, And Write To New Csv"