PDA

View Full Version : how to I write python 3 csv file dictreader to dict writer



lance bermudez
April 30th, 2015, 06:20 AM
Ok what i want to do is take the info out the print function and put it into another csv file.

python3 script


import csv
with open('C:/Users/lance/Videos/11001.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
#I want this print fieldnames written to another file with the data.
print('\n',
'S, Last name, First Name, Grade\n'.upper(),
row['OrgDefinedId'],
row['Last Name'],
row['First Name'],
'{:.2f}'.format(float(row['Calculated Final Grade Numerator'])))#this line has to keep its formatting


I'm stuck I do not know how to proceed. Was thinking of something like this


import csv
import operator

with open('C:/Users/lance/Videos/11001.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
snumber = row['OrgDefinedId']
last = row['Last Name']
first = row['First Name']
grade = '{:.2f}'.format(float(row['Calculated Final Grade Numerator']))
print(snumber)

csvfile = open('eggs.txt','w')
csvfile.write (snumber)
csvfile.write (',')

But this does not write out all the snumber that is seen in the print just the first line.

Vaphell
May 1st, 2015, 02:07 AM
you are opening the file for each line and do so the 'w' flag which is overwrite instead of append ('a')


with open( 'out.txt', 'w' ) as ofile:
with open( 'in.txt' ) as ifile:
for line in ifile:
ofile.write( line )

edit: you are using the same file (csvfile) but the comment says you want output in another file?