In python 2.7 - How to read data from csv, reformat data, and write to new csv -


I'm a programming knob, and I'm stuck ... my goal is to open a CSV file (an export program A ), To reproduce the data, and to write it in a new CSV file (to import program B) I know that my code is not very good, though it works as long as They do not write in the new CSV file. It only writes the last line of data from the old CSV. Enter

  CSV print 'file location or press Enter for the default location.' APath = raw_input ('& gt;') # User Path ifPath == '': aPath = '/ default / path / to / file' # Default path aFile = raw_input ('Enter file name' '#) File name if AFile == '': aFile = 'orginal.csv' # Default file name myCSV = csv.DictReader (open (aPath + aFile, 'r'), delimiter = ',') print myCSV.fieldnames # field name Verify mysv at p: w = csv.writer (open ("output.csv", "w")) try: p = dict ((k, v) k, p.iteritems () in v if v. Except AttributeError, e: Print and Print P exception exception () # output.csv pID = p.get ('last name') except for lower ()! = 'Null') Re-formatting the files [- 4:] Get pName = p ('Last name') [: - 4]. Strip () + ',' + p.get ('first name') pDate = p.get ('Start Time') [: - 5] get pBlank = p ('') pCourse = p.get ('Assigned through') pScore = p.get ('score') [: - 1] #Printing of new columns to print PID, PN, PDT, PBANK, pekors, PBANK, PBANK, PSCOR # is not working ... only wri Orginal.csv output to the last line. Csv w.writerow ([pID, pName, Last line of pDate, pBlank, pCourse, pBlank, pBlank, pScore])  

The following modified code should work:

  import csv print 'Enter file location or hit enter for the default location . ' APath = raw_input ('& gt;') # User Path ifPath == '': aPath = '/ default / path / to / file' # Default path aFile = raw_input ('Enter file name' '#) File name if AFile == '': aFile = 'orginal.csv' # The default file name is myCSV = csv.DictReader (open (aPath + aFile, 'r'), delimiter = ',') myCSV.fieldnames print # verify field names Open in myCSV as open ("output.csv", "wb") asile: for w = csv.writer (forile) p: try: p = dict (for k, v) k, v P.iteritems () to reformat columns of data for output.csv pID = p.get ('last name') Exception (): v.lower ()! = 'Null') [- 4:] PName = p.get ('last name') [: - 4] .strip () + ',' + P.get ('first name') pDate = p.get ('start time') [: - 5] PBlank = p.get ('') assigned via pCourse = p.get (' ') PScore = p.get (' score ') [: - 1] # verifies new column Print PID, PNM, PDT, PBANK, Peacres, PBANK, PBANK, PSCOR # is not working ... Only output to the last line of orginal.csv.csv w.writerow ([pID, pName, pDate, pBnank, pCourse writes, Pbank, Pbank, Pscore] )  

We have changed the circumstances:

  1. We have used the operator with with with < Working with opening and closing files using / code> is usually the accepted protocol, unless otherwise applicable because it handles files with other things properly.
  2. Instead of wb your second argument on w as open . This will allow you to open file in write-binary mode. See for reference.

Hope it helps.


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -