Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parsing an MBOX file
#1
I have a client who wants to be able to parse and extract the message portions from an mbox (email) file. The mbox I have as an example has huge sections of what appears to be encrypted text. The code below extracts the text portions correctly, I think, but I'm not sure if the code is supposed to handle the seemingly-encrypted mbox text or if the mbox just has encrypted portions that can't be read.

Does the code below look correct to read/extract mbox data: to, from, subject, and body?

Thanks very much in advance,

-O

---


import os
import mailbox
import sys
import pprint

print("Reading emails:")

mbox_file = "/Users/oliver/Desktop/mbox"

print("Processing " + mbox_file)
mbox = mailbox.mbox(mbox_file)

for key in mbox.iterkeys():

    try:
        message = mbox[key]
    except mbox.errors.MessageParseError:
        continue  # The message is malformed. Just leave it.

    print("From: " + message['from'])
    print("To: " + message['to'])
    print ("Subject: " + str(message['Subject']))
    print("-----------------------------")
    print("Body\n")
    print (message)

    print("********************************************")
Reply
#2
Looks good.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,667 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,577 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Parsing xml file deletes whitespaces. How to avoid it? Paqqno 0 1,010 Apr-01-2022, 10:20 PM
Last Post: Paqqno
  Parsing a syslog file ebolisa 11 4,002 Oct-10-2021, 05:15 PM
Last Post: snippsat
Thumbs Up Parsing a YAML file without changing the string content..?, Flask - solved. SpongeB0B 2 2,222 Aug-05-2021, 08:02 AM
Last Post: SpongeB0B
  File Name Parsing millpond 5 3,529 Aug-26-2020, 08:04 AM
Last Post: bowlofred
  Error while parsing tables from docx file aditi 1 3,669 Jul-14-2020, 09:24 PM
Last Post: aditi
  help parsing file aslezak 2 2,195 Oct-22-2019, 03:51 PM
Last Post: aslezak
  Python Script for parsing dictionary values from yaml file pawan6782 3 4,908 Sep-04-2019, 07:21 PM
Last Post: pawan6782
  parsing complex text file anna 1 2,040 Apr-10-2019, 09:54 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020