0

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])
3
  • 4
    Your error message seems to refer to a line which isn't in your code. If you're getting an IndexError when you access row[3], then simply add print row before the access and see what line is giving you trouble. For the code you've posted, after you've finished the for col in reader: loop, you're at the end of the file, so I don't see how next(reader) or for row in reader: would work/do anything. Commented Oct 11, 2013 at 19:30
  • I agree with @DSM, you should post your actual code, or something that behaves the same way. Commented Oct 11, 2013 at 19:39
  • Are you using the code from your earlier question: stackoverflow.com/questions/19324968/python-csv-copy-column ? If so, maybe you should have added to that question rather than start a new one. Commented Oct 11, 2013 at 19:59

1 Answer 1

0

It looks like you edited the code you received in your earlier answer.

Change

email = email.split('@', 1)[0] + '@hotmail.com'

to

email = row[3].split('@', 1)[0] + '@hotmail.com'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.