【发布时间】: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怎么办?