Python CSV脚本

时间:2013-10-11 19:27:03

标签: python csv

我正在尝试通过复制col#3添加新列,然后将@hotmail附加到新列

这是脚本,唯一的问题是它不会完成处理输入文件,它只在输出文件中显示61409行,而在输入文件中有61438行。
此外,还有一条错误消息(输入文件末尾没有空行):
email = row [3] IndexError:列表索引超出范围

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])

1 个答案:

答案 0 :(得分:0)

您似乎编辑了之前回答中收到的代码。

更改

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

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