解析电子邮件时仅使用Python进行回复时出错 [英] Error parsing emails for only replies with Python

查看:33
本文介绍了解析电子邮件时仅使用Python进行回复时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要解析的电子邮件的csv,在这里上传

I have this csv with a bunch of emails that I want to parse, uploaded here

使用此图书馆,并感谢

Using this library and thanks to this answer, we have this loop that would go over the CSV and apply the cleaning function EmailReplyParser.parse_reply(email_message) to each 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)
    # need to skip the title
    title = reader.__next__()
    for row in reader:
        EmailReplyParser.parse_reply(row[0].split()[-1])
    writer.writerows(reader)

但是,这不起作用.

它循环得很好,但是电子邮件不会被清除.当我尝试使用从CSV粘贴的单个邮件副本进行尝试时,

It loops just fine, but emails are not cleaned. When I try it with a single message copy pasted from the CSV,

email = """I don't have an owl

On Saturday 18 June 2016, Hogwarts School of Witchcraft and Wizardry <
no-reply@wufoo.com> wrote:

> HOGWARTS SCHOOL of WITCHCRAFT and WIZARDRY
>
> Headmaster: Albus Dumbledore
> (Order of Merlin, First Class, Grand Sorc., Chf. Warlock,
> Supreme Mugwump, International Confed. of Wizards)
>
> Dear Student,
>
> We are pleased to inform you that you have been accepted at Hogwarts
> School of Witchcraft and Wizardry. Please find enclosed a list of all
> necessary books and equipment.
>
> Term begins on 1 September. We await your owl by no later than 31 July.
>
>
> Yours sincerely,
>
> [image: image]
>
> Minerva McGonagall
>
> Deputy Headmistress
>
> Here is your ticket for the Hogwarts Express:
>
> [image: image]
>"""

它给了我正确的结果,就像这样:

It gives me the right result, like so:

EmailReplyParser.parse_reply(email)
Out[11]: "I don't have an owl"

为什么无法正确读取CSV?(我已经上传了CSV,因此无需下载即可尝试使用.)

Why is the CSV not being read correctly? (I've uploaded the CSV so that it can be tried without downloading).

推荐答案

我通过读取csv来简化解析csv行的方式,如下所示:

I simplified the way to parse a csv row by reading your csv like this:

with open('D:/Harry_Potter.csv', encoding="utf8") as inf:
    reader = csv.reader(inf)

然后更改您写入csv的循环.代码(更改用于打开/关闭文件的参数):

Then change your loop for writing to the csv. Code (change arguments for opening/closing the file):

from email_reply_parser import EmailReplyParser
import csv
with open('hp.csv', encoding="utf8") as inf:
    reader = csv.reader(inf)
    with open('out.csv', 'w') as outf:
        # need to skip the title
        title = reader.__next__()
        for row in reader:
            # you need to store the return value from 'parse_reply'
            get_reply = EmailReplyParser.parse_reply(row[-1]) 
            # check what reply you get here
            print("Reply:", get_reply)
            outf.write(str(get_reply))

这篇关于解析电子邮件时仅使用Python进行回复时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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