我有一个格式化的文本文件,由outlook邮件组成。你知道吗
我试图解析From、主题(多个字段),然后阅读其余内容,直到下一封新电子邮件由新From指示:
首先,我试图强行使用它,因为这是对概念验证的测试,然而,我只收到链中最后一封电子邮件。你知道吗
l = []
with open(r'transcripts.txt', 'r') as transcripts:
for line in transcripts:
is_new_subject = line.lower().startswith('from')
if is_new_subject:
record = {}
record['from'] = line.split(':')[1]
for line in transcripts:
if line.lower().startswith('subject'):
subject = line.split(':')[1]
record['subject'] = subject
split_it = subject.split('.')
record['show'] = split_it[0]
record['air_date'] = split_it[1]
record['hour'] = split_it[2]
record['content'] = ""
for line in transcripts:
record['content'] += line
is_new_subject = line.lower().startswith('from')
if is_new_subject:
l.append(record)
break
with open('output.json', 'w') as outfile:
json.dump(l, outfile, indent=4)
有什么想法,我要从头开始重新做
你应该试试Email Parser。这很容易使用。由于某些原因,此电子邮件不适用于多部分电子邮件。所以我使用了@Max Paymar创建的split函数。谢谢@Max Paymar。你知道吗
你的代码有点难读,我想如果你把它分解成函数,调试起来会容易得多。另外,我建议使用python的re库进行这种类型的文本处理,因为它比只测试静态字符串灵活得多。例如:
在仔细查看代码之后,很明显,循环逻辑肯定是您遇到问题的地方。你知道吗
相关问题 更多 >
编程相关推荐