没有以前的对话如何解析电子邮件正文?

时间:2017-02-09 13:29:21

标签: regex python-2.7 parsing

在我们与其他人交谈时的gmail中,最后每封电子邮件与之前的对话相连接。在python imaplib 库的帮助下,我得到了如下的电子邮件正文。现在我想删除以前的对话,只获得主要信息......

输入:

--------------------Now i Have--------------------------
Dear vaia,


Sale order fail to sync when it contain Generic Product. ....need to little
investigate about it.
This is a issue which is occurred if any product have salePrice of greated
then 2 decimal value like 33.34500 etc.
Now POS only use @ decimal values like 33.34 so please aware about this
about configuring prism to have always 2 decimal digits.


On Thu, Jan 26, 2017 at 9:23 PM, Abu Shaed Rimon <rimon@divine-it.net>
wrote:

>
> Dear Concern,
>
> Observation after update:-
>
> -- "+" sign working for add customer and Product but this button also
>
> Thank You.
>
>
> *...Best Regards,*
> http://www.divineit.net
>
>
> On Thu, Jan 26, 2017 at 5:44 PM, Khirun Nahar Urmi <urmi@divine-it.net>
> wrote:
>
>> Dear Rimon vaia,
>>
>>
>> Please take an update from git
>>
>> On Thu, Jan 26, 2017 at 3:24 PM, Abu Shaed Rimon <rimon@prismerp.net>
>> wrote:
>>
>>> Dear Concern,
>>>
>>> Please take a review about the mentioned observation in following :-
>>> *Helpdesk:* http://support.divineit.net
>>>

输出:

---------------------My Expectation-------------------------
Dear vaia,


Sale order fail to sync when it contain Generic Product. ....need to little
investigate about it.
This is a issue which is occurred if any product have salePrice of greated
then 2 decimal value like 33.34500 etc.
Now POS only use @ decimal values like 33.34 so please aware about this
about configuring prism to have always 2 decimal digits.

1 个答案:

答案 0 :(得分:1)

你可以这样:

import re

with open(file, 'r') as f:
    print re.findall(r'^.*?(?=On \w{3},)', f.read(), re.DOTALL)[0].strip()

输出:

Dear vaia,


Sale order fail to sync when it contain Generic Product. ....need to little
investigate about it.
This is a issue which is occurred if any product have salePrice of greated
then 2 decimal value like 33.34500 etc.
Now POS only use @ decimal values like 33.34 so please aware about this
about configuring prism to have always 2 decimal digits.

正则表达式:

^.*?(?=On \w{3},) - 匹配从On \w{3},模式开始到第一次出现的所有内容。

re.DOTALL也会使.匹配换行符。