【问题标题】:Error parsing emails for only replies with Python仅使用 Python 回复解析电子邮件时出错
【发布时间】:2021-04-04 08:07:33
【问题描述】:

我有这个 csv,里面有一堆我想解析的电子邮件,uploaded here

使用这个library 并感谢this answer,我们有这个循环将遍历CSV 并将清理功能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)
    # need to skip the title
    title = reader.__next__()
    for row in reader:
        EmailReplyParser.parse_reply(row[0].split()[-1])
    writer.writerows(reader)

但是,这不起作用。

它可以正常循环,但没有清理电子邮件。当我尝试使用从 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]
>"""

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

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

为什么无法正确读取 CSV? (我已上传 CSV 文件,无需下载即可试用)。

【问题讨论】:

  • 你能从 csv 中添加实际产生差异/问题的行吗?
  • 我不确定你的意思,我不认为它是一个特定的行。但是,我想知道这是否是问题所在。我们用第一个with 读入的文件是否保持打开的时间足够长,可以被第二个循环解析?如果它只是试图解析的新的、空的clean.csv 怎么办?

标签: python csv email


【解决方案1】:

我通过像这样读取您的 csv 来简化解析 csv 行的方法:

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

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

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

【讨论】:

  • 所以我有这个:pastebin.com/vCwibPqL 但我在title = reader.__next__() 得到了一个ValueError: I/O operation on closed file.
  • 在您打开 csv 文件的 with 块中执行此操作。
  • 对不起,不断的问题,我对此很陌生。你是这个意思吗? pastebin.com/rrNuqRhS 我尝试删除 clean.csv 位,以简单地覆盖现有文件,但没有看。此外,使用print(get_reply),我仍然可以在输出中看到带引号的文本,所以我认为字符串无论如何都没有被正确解析。
  • @Aman 检查编辑,我假设您只需将一个字符串(一行)写入输出 csv。此外,输入的 csv 有一些问题,所以循环会停止,你可能需要修复你的 csv。
  • 抱歉没有早点回来,我的笔记本电脑不在身边。它没有按预期工作,而不是单行,我得到的文本被分成多列(见这里i.imgur.com/CipZXCy.png)。今天早上我最终不得不手动复制粘贴 300 封电子邮件。我认为问题是因为Message 列中的每个条目都有多行,也许 csv 模块没有正确读取?
猜你喜欢
  • 2015-03-21
  • 1970-01-01
  • 2011-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-04
相关资源
最近更新 更多