[英]Error "list indices must be integers or slices, not str" while looping
我试图只解析对我的电子邮件的回复,这些回复存储在 CSV 文件中。 我正在使用这个似乎适合这样做的库。 我的 CSV 列如下所示:
Date From Name From Address To Subject Message
我想要做的是阅读message
列,执行清理 function 即EmailReplyParser.parse_reply(email_message)
并将其替换为清理后的电子邮件。
这就是我现在所拥有的:
from email_reply_parser import EmailReplyParser
import csv
with open('D:/Harry_Potter.csv', encoding="utf8") as inf:
reader = csv.reader(inf.readlines())
with open('D:/clean.csv', 'w') as outf:
writer = csv.writer(outf)
for row in reader:
EmailReplyParser.parse_reply(row['Message'])
writer.writerows(reader)
这是我收到的错误: TypeError: list indices must be integers or slices, not str
。
我怎样才能解决这个问题?
你的循环应该是这样的:
with open('D:/clean.csv', 'w') as outf:
writer = csv.writer(outf)
# need to skip the title
title = reader.__next__()
for row in reader:
EmailReplyParser.parse_reply(row[0].split()[-1])
writer.writerows(reader)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.