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
  Reading an ASCII text file and parsing data... oradba4u 2 2,801 Jun-08-2024, 12:41 AM
Last Post: oradba4u
Video doing data treatment on a file import-parsing a variable EmBeck87 15 7,778 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Modify values in XML file by data from text file (without parsing) Paqqno 2 3,968 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Parsing xml file deletes whitespaces. How to avoid it? Paqqno 0 2,185 Apr-01-2022, 10:20 PM
Last Post: Paqqno
  Parsing a syslog file ebolisa 11 8,303 Oct-10-2021, 05:15 PM
Last Post: snippsat
Thumbs Up Parsing a YAML file without changing the string content..?, Flask - solved. SpongeB0B 2 3,830 Aug-05-2021, 08:02 AM
Last Post: SpongeB0B
  File Name Parsing millpond 5 8,295 Aug-26-2020, 08:04 AM
Last Post: bowlofred
  Error while parsing tables from docx file aditi 1 7,078 Jul-14-2020, 09:24 PM
Last Post: aditi
  help parsing file aslezak 2 3,760 Oct-22-2019, 03:51 PM
Last Post: aslezak
  Python Script for parsing dictionary values from yaml file pawan6782 3 7,590 Sep-04-2019, 07:21 PM
Last Post: pawan6782

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.