473,395 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to divide file into separate lists?

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, 431 views)
Nov 24 '10 #1
2 2333
dwblas
626 Expert 512MB
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
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

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

Similar topics

1
by: Viswa | last post by:
PythonGurus, I would like to read the ascii data from a file with many columns into a list. I want the type to be float. data=open('dat1.dat','r') lines=data.readlines() print lines
3
by: Dfenestr8 | last post by:
os.listdir("path") returns the names of all the items in a directory ok, but I need to know whether the item is a file or a directory. Can anybody tell me how this could be done?
8
by: Rick Brandt | last post by:
Just got a new PC at work (woo hoo) and I'm wondering if the way the various versions of Access were installed has left it in some kind of incorrect state. In an app I am working on I added a...
4
by: Anders Eriksson | last post by:
Hello! I'm using VC++ 7.1 and MFC. In a header file that is located in a different directory that the main project I include a header file that is located in the main project directory. The...
4
by: ESPN Lover | last post by:
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks. using System; using System.IO; class Test { public static void...
3
by: R. P. | last post by:
Subject: XSLT to transform a flat XML file into a structured text file I have an XML file that lists the PDF file segment names and titles of a larger document and looks something like this: ...
6
by: tomtown.net | last post by:
Hello I'm trying to get a single line removed from a text file using a search pattern (pretty simple: if line contains "NODE1") -> remove line). To achieve this I's like to operate with only the...
13
by: Ørjan Langbakk | last post by:
I wanna make a file that holds the complete pricelist for a small webshop (yes, I know that a database in the background would be a lot simpler, but that is not an option today, unfortunately). ...
9
Dormilich
by: Dormilich | last post by:
Hi, I could need some advice. In my script I'm reading a language translation file (some hundred entries). which file I select depends on the user selections (i.e. $_GET). my problem is currently,...
6
Avatar19
by: Avatar19 | last post by:
Hi, I would like to compare elements in two seperate Lists for example: List1=, , ] List2=, ] What I understand is that the syntax should look like this: List3 for i in List2: for j in...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.