I'm trying to add a new column by copying col#3 and then append @hotmail to the new column
Here is the script, only problem is that it will not finish processing the input file, it only show 61409 rows in the output file, whereas in the input file there are 61438 rows.
Also, there is an error message (the input file does not have empty line at the end):
email = row[3]
IndexError: list index out of range
inFile = 'c:\\Python27\\scripts\\intake.csv'
outFile = 'c:\\Python27\\scripts\\final.csv'
with open(inFile, 'rb') as fp_in1, open(outFile, 'wb') as fp_out1:
writer = csv.writer(fp_out1, delimiter=",")
reader = csv.reader(fp_in1, delimiter=",")
for col in reader:
del col[6:]
writer.writerow(col)
headers = next(reader)
writer.writerow(headers + ['email2'])
for row in reader:
if len(row) > 3:
email = email.split('@', 1)[0] + '@hotmail.com'
writer.writerow(row + [email])
IndexErrorwhen you accessrow[3], then simply addprint rowbefore the access and see what line is giving you trouble. For the code you've posted, after you've finished thefor col in reader:loop, you're at the end of the file, so I don't see hownext(reader)orfor row in reader:would work/do anything.