I want python to extract only the specified parts of the body from the mail file.

Asked 2 years ago, Updated 2 years ago, 108 views

Thank you for your help.
In response to an email I sent in the past,
There is an e-mail file with my writing written on it.

 ^^--------------------------------------------------------------------------------------    
Details of 月/ 日:
[Place] △△ Hall
[Possessions] Nothing in particular
 Remarks
  Please use public institutions on the day.
 ---------------------------------------------------^≫
Please come by all means.




----- Original Message -----
From: 0×
Sent—Monday
To: ☆×
Subject: About the schedule

Hi, How are you?
Please let me know the schedule as below.
≪^------------------ Dear 〇 へ ----------------------------------
 Details of 月/ 日:
[Place] x Hall
[Possessions] Nothing in particular
 Remarks
  〇〇〇〇〇〇〇〇〇〇
 ---------------------------------------------------^≫

"

is the content of ""Dear XX"" at the top."

△△Hall
Nothing in particular
Please use public institutions on the day.

You only want to print on the command prompt, so you can see what others have asked you in the past, or
I wrote this based on the answers I received when I asked about regular expressions.

import ExtractMsg
import re
from glob import glob
import sys
args=sys.argv

for filename in glob(r"{}/*.msg".format(args[1])):
    msg = ExtractMsg.Message(filename)
    msg_message=msg.body
    m=re.search('(.*)(^ ^^.*-)(.*)(^-.*^)(.*)',msg_message,flags 
(re.DOTALL | re.MULTILINE))
    ifm:
        x = re.sub('\[.*\]', "", m.group())
        print(x)
    else:
        pass

There is no output and I am in trouble.Below

〇Details of 月/ 日:
[Place]××hole
[Possessions] Nothing in particular
Notes
〇 〇 br 〇 <
does not print if it is written in the email.

I wish I could code
I don't know how to do it.
I would appreciate it if you could let me know.

python python3 regular-expression

2022-09-30 19:50

1 Answers

If you don't say you have to use regular expressions, you can implement them in a straightforward way.I haven't debugged it, so I haven't checked it yet.

with open(file, "r")f:
  lines=f.readlines()

stat = 0

For line in lines:
  if stat == 0:
    ifline.startwith("[location]"):
      print line.trip("[location]")
      stat = 1
  if stat == 1:
    print line.strip("[Possessions]")
    stat = 2
  if stat == 2:
    # skip "Notes"
    stat = 3
    continue
  if stat == 3:
    print line
    stat = 0


2022-09-30 19:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.