我有一个csv,里面有一堆我想解析的电子邮件,上传到这里
使用这个库,由于这个答案,我们有了这个循环,它将遍历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简化了解析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))