简体   繁体   中英

Error "list indices must be integers or slices, not str" while looping

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

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.

This is what I have right now:

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)

This is the error I'm getting: TypeError: list indices must be integers or slices, not str .

How can I fix this?

Your loop should be like this:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM