I'm having trouble with some code at the moment, I can't quite figure out how to add the year (last 4 digits of multiple filenames) as a sort of 'ID' field, using a for-loop? I know there are other ways of doing this but I would like to try this way as I'm learning for-loops.
Code I have so far:
import csv
def extract_names(filename):
inF = open(filename, 'rU')
csvF = csv.reader(inFile, delimiter=',')
# Initialization
results = []
rowNum = 0
for row in csvFile:
if rowNum != 0: #no need for first row#
#This is where the results list is appended #
records.append((row[0], row[1], "Boy"))
records.append((row[2], row[3], "Girl"))
rowNum += 1
inF.close()
return(results)
#### Start main program #####
filenames = ('file2010.csv',
'file2011.csv',
'file2012.csv',
'file2013.csv',
'file2014.csv')
outF = open('fileAll.csv','wb')
csvF_out = csv.writer(outFile, delimiter=',')
for filename in filenames:
name, ext = filename.split('.')
year = name[-4:]
results = extract_names(filename)
for line in results:
line.insert(0,year)
print("Write in csv file...")
outF.close()
Desired output:
2010 | Y | X | Z