错误“列表索引必须是整数或切片,而不是str".在循环时 [英] Error "list indices must be integers or slices, not str" while looping

查看:78
本文介绍了错误“列表索引必须是整数或切片,而不是str".在循环时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试仅解析对电子邮件的答复,这些答复存储在CSV文件中.我正在使用此库,该库似乎适合于这样做.我的CSV列如下所示:

I'm trying to parse only the replies to my emails, which are stored in a CSV file. I am making use of this library which seems to be geared at doing that. My CSV columns look like this:

Date    From Name   From Address   To   Subject    Message

我想做的是阅读 message 列,执行清洁功能,即 EmailReplyParser.parse_reply(email_message)并将其替换为已清洁的电子邮件.

What I want to do is read the message column, perform the cleaning function which is EmailReplyParser.parse_reply(email_message) and replace it with the cleaned emails.

这就是我现在拥有的:

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:列表索引必须是整数或切片,而不是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)

这篇关于错误“列表索引必须是整数或切片,而不是str".在循环时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆