I am trying to read a csv-style file and create another one. Here is the (simplified) code.
import os
import csv
import sys
fn = 'C:\mydird\Temp\xyz'
ext = '.txt'
infn = fn+ext
outfn = fn+'o'+ext
infile = open(infn, newline='')
outfile = open(outfn, 'w', newline='')
try:
reader = csv.reader(infile, delimiter=',', quotechar='"') # creates the reader object
writer = csv.writer(outfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for row in reader: # iterates the rows of the file in orders
if reader.line_num == 1 :
print("Header")
writer.writerow(['Date',
'XY']) # ####### Does not work
else:
# Do something
print(row[0],
row[3]) # ####### Works
writer.writerow([row[0],
row[3]]) # ####### Does not work
finally:
infile.close() # closing
sys.exit(0))
Neither of the writerow statements generate output.
The file is created, but is empty.
The print statement creates 'expected' ouput.
I have also tried already csv.DictWriter with no success either.
I have looked at the various simlar questions, but can't see any difference.
I am using Py 3.3.3 on a win 7 machine.
EDIT:
the writer got lost be code simplification
writeror close theoutfile