473,385 Members | 1,720 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,385 software developers and data experts.

word frequency

I'm trying to print email address and each of their frequency on it's right by reading a text file. The counting part is correct but it prints other things as well. I'm not sure how to solve this. Hope someone could enlighten me. Thank you very much.

Expand|Select|Wrap|Line Numbers
  1. import string
  2. fname = raw_input("Enter a file name: ")
  3. if len(fname) == 0 :
  4.     print "Assuming mbox-short.txt"
  5.     fname = "mbox-short.txt"
  6.  
  7. try:
  8.     infile = open(fname, "r")
  9. except:
  10.     print "File not found:", fname
  11.     exit()
  12.  
  13. counts = {}
  14. for line in infile:
  15.  
  16.     words = string.split(line)
  17.  
  18.  
  19.  
  20.     if (len (words) > 0 and words[0] == 'From'):
  21.         for w in words:
  22.             counts[w] = counts.get(w,0) + 1
  23.             print w, counts [w]
  24.  
  25.  
  26.  
Oct 1 '08 #1
3 3087
bvdet
2,851 Expert Mod 2GB
Post a representative sample of your text file, and we can help you solve the problem.
Oct 1 '08 #2
From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
Return-Path: <postmaster@collab.sakaiproject.org>
Received: from murder (mail.umich.edu [141.211.14.90])
by frankenstein.mail.umich.edu (Cyrus v2.3.8) with LMTPA;
Sat, 05 Jan 2008 09:14:16 -0500
X-Sieve: CMU Sieve 2.3
Received: from murder ([unix socket])
by mail.umich.edu (Cyrus v2.2.12) with LMTPA;
Sat, 05 Jan 2008 09:14:16 -0500
Received: from holes.mr.itd.umich.edu (holes.mr.itd.umich.edu [141.211.14.79])
by flawless.mail.umich.edu () with ESMTP id m05EEFR1013674;
Sat, 5 Jan 2008 09:14:15 -0500
Received: FROM paploo.uhi.ac.uk (app1.prod.collab.uhi.ac.uk [194.35.219.184])
BY holes.mr.itd.umich.edu ID 477F90B0.2DB2F.12494 ;
5 Jan 2008 09:14:10 -0500
Received: from paploo.uhi.ac.uk (localhost [127.0.0.1])
by paploo.uhi.ac.uk (Postfix) with ESMTP id 5F919BC2F2;
Sat, 5 Jan 2008 14:10:05 +0000 (GMT)
Message-ID: <200801051412.m05ECIaH010327@nakamura.uits.iupui.e du>
Mime-Version: 1.0
Content-Transfer-Encoding: 7bit
Received: from prod.collab.uhi.ac.uk ([194.35.219.182])
by paploo.uhi.ac.uk (JAMES SMTP Server 2.1.3) with SMTP ID 899
for <source@collab.sakaiproject.org>;
Sat, 5 Jan 2008 14:09:50 +0000 (GMT)
Received: from nakamura.uits.iupui.edu (nakamura.uits.iupui.edu [134.68.220.122])
by shmi.uhi.ac.uk (Postfix) with ESMTP id A215243002
for <source@collab.sakaiproject.org>; Sat, 5 Jan 2008 14:13:33 +0000 (GMT)
Received: from nakamura.uits.iupui.edu (localhost [127.0.0.1])
by nakamura.uits.iupui.edu (8.12.11.20060308/8.12.11) with ESMTP id m05ECJVp010329
for <source@collab.sakaiproject.org>; Sat, 5 Jan 2008 09:12:19 -0500
Received: (from apache@localhost)
by nakamura.uits.iupui.edu (8.12.11.20060308/8.12.11/Submit) id m05ECIaH010327
for source@collab.sakaiproject.org; Sat, 5 Jan 2008 09:12:18 -0500
Date: Sat, 5 Jan 2008 09:12:18 -0500
X-Authentication-Warning: nakamura.uits.iupui.edu: apache set sender to stephen.marquard@uct.ac.za using -f
To: source@collab.sakaiproject.org
From: stephen.marquard@uct.ac.za
Subject: [sakai] svn commit: r39772 - content/branches/sakai_2-5-x/content-impl/impl/src/java/org/sakaiproject/content/impl
X-Content-Type-Outer-Envelope: text/plain; charset=UTF-8
X-Content-Type-Message-Body: text/plain; charset=UTF-8
Content-Type: text/plain; charset=UTF-8
X-DSPAM-Result: Innocent
X-DSPAM-Processed: Sat Jan 5 09:14:16 2008
X-DSPAM-Confidence: 0.8475
X-DSPAM-Probability: 0.0000

Details: http://source.sakaiproject.org/viewsvn/?view=rev&rev=39772

Author: stephen.marquard@uct.ac.za
Date: 2008-01-05 09:12:07 -0500 (Sat, 05 Jan 2008)
New Revision: 39772

Modified:
content/branches/sakai_2-5-x/content-impl/impl/src/java/org/sakaiproject/content/impl/ContentServiceSqlOracle.java
content/branches/sakai_2-5-x/content-impl/impl/src/java/org/sakaiproject/content/impl/DbContentService.java
Log:
SAK-12501 merge to 2-5-x: r39622, r39624:5, r39632:3 (resolve conflict from differing linebreaks for r39622)

----------------------
This automatic notification message was sent by Sakai Collab (https://collab.sakaiproject.org/portal) from the Source site.
You can modify how you receive notifications at My Workspace > Preferences.



From louis@media.berkeley.edu Fri Jan 4 18:10:48 2008
Return-Path: <postmaster@collab.sakaiproject.org>
Received: from murder (mail.umich.edu [141.211.14.97])
by frankenstein.mail.umich.edu (Cyrus v2.3.8) with LMTPA;
Fri, 04 Jan 2008 18:10:48 -0500
X-Sieve: CMU Sieve 2.3
Received: from murder ([unix socket])
by mail.umich.edu (Cyrus v2.2.12) with LMTPA;
Fri, 04 Jan 2008 18:10:48 -0500
Oct 1 '08 #3
bvdet
2,851 Expert Mod 2GB
I'm not sure what your problem is, but this will print the email addresses and the number of times they occur:
Expand|Select|Wrap|Line Numbers
  1. fn = 'text.txt'
  2. f = open(fn)
  3.  
  4. emailDict = {}
  5. for line in f:
  6.     if line.lower().startswith('from'):
  7.         try:
  8.             email = line.split()[1]
  9.             emailDict[email] = emailDict.get(email,0)+1
  10.         except:
  11.             pass
  12.  
  13. f.close()
  14.  
  15. for key in emailDict:
  16.     print '%s: %d' % (key, emailDict[key])
I did little to your code except pull the print statements outside the loop.
Oct 1 '08 #4

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

Similar topics

2
by: Isaac Gouy | last post by:
Seems like there's a problem with the word-frequency program written for "The Great Computer Language Shootout" - it's using 161,500KB which is far more than other programs: ...
4
by: Mickel Grönroos | last post by:
Hi! I'm fiddling with Python for Windows (normally using Linux) and as a part of that I want to try to make a simple word frequency list generator to be used in conjunction with Microsoft Word....
4
by: fhmm | last post by:
I am an intermediate programmer and i tried to solve the following programme but with no success the programme is a simple text analyzer that read an input file and cumpute the occurence and...
4
by: ajkadri | last post by:
Folks, I have written a word frequency counter program in python that works well for .txt files; but it cannot handle .DOC files. Can someone help me to resolve this issue???
1
by: tnt84 | last post by:
I want to write a program that reads a text file and prints out the word frequencies using structure but I don't know exactly what the word frequency is and how can I write that program using...
5
by: krisbee1983 | last post by:
Hello to all, I'm beginer in learning Python I wish somebody help me with solving this problem. I would like to read all text files wchich are in some folder. For this text files I need to make...
2
by: fatenn | last post by:
I need make from the Oracle Text index of the "word-frequency histogram", this is list of the tokens in this index, where each token contains the list of documents that contain that token and...
1
by: ADHaughton | last post by:
i have an array pages = ; the user is asked to enter a word 'w', which is then searched for in the above array. I want the frequency of that word in pages outputed, then the frequency of the same...
8
by: Andrew Savige | last post by:
I'm learning Python by reading David Beazley's "Python Essential Reference" book and writing a few toy programs. To get a feel for hashes and sorting, I set myself this little problem today (not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.