By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

How to divide file into separate lists?

P: 2
How can I divide a file with emails divided with headers like this:

From: jdakl@hotmail.com
Sent: 7th aprill 2099
To: hjfdaso@hotmail.com

email text

...into separate lists in one big list containing the email and the header?

the point is that im supposed to check each email for spam words and give points for each spam word... and then put the spam emails from the email file into a spam email list and file.. and all of the normal emails from the email file into a file as well as list for normal emails...

HELP ANYONE ?? :D :D :D

i attached what i ve done so far. You can ignore the ## comments.. it's just stuff i've done and decided to ignore
Attached Files
File Type: txt spam.txt (2.9 KB, 359 views)
Nov 24 '10 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 624
Append the records to a list. Everytime the "From:" record is found, send the list to a function to process the previous group any way you want, and re-declare the list as empty. Pseudo-code:
Expand|Select|Wrap|Line Numbers
  1. def process_group(list_in, normal_fp, spammer_fp):
  2.     if len(list_in):      ## Why do we want this line?
  3.         spammer = False
  4.         print list_in
  5.         if spam:
  6.             spammer = True
  7.  
  8.         ## all records processed
  9.         if spammer:
  10.             ## write records to spammer file
  11.         else:
  12.             ## write records to normal file
  13.  
  14. normal_fp = open("normal.txt", "w")
  15. spammer_fp = open("spammer.txt", "w")
  16.  
  17. group_list = []
  18. for rec in fname:
  19.     if rec.strip().startswith("From:"):
  20.         process_group(group_list, normal_fp, spammer_fp)
  21.         group_list = []
  22.     group_list.append(rec)
  23.  
  24. process_group(group_list, normal_fp, spammer_fp)     ## process final group 
Nov 24 '10 #2

P: 2
thank u very much 4 replying
I dont get one thing though. where does the startswith() function come from ? in line 19
and what does the process_group() function do ?
I'm sorry if i seem a bit lost im new to this
Nov 24 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.