Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > Python

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-22-2011, 05:26 AM   PM User | #1
SunJune
New Coder

 
Join Date: Dec 2010
Posts: 19
Thanks: 5
Thanked 0 Times in 0 Posts
SunJune is an unknown quantity at this point
count the item in a list without any numbers and quotations

Hi Everyone,

I have a file as below and want to have a output in the following format (second block). So far I have been able to get the result (please see third block). My code is in last block.

As you can see I am still getting the numbers as well in my output, how can I get rid of that? I searched a lot, but couldnt able to get it.

I still have to write the code for counting the items, but need to get this working first.

Please advise..


Code:
Now
is the time
for all good-men
to come to the-
aid of -their
party. NOW IS THE TIME FOR all Good men to
come to the AID of their party. party-their now-is time-the-for all-good
men to -come aid of party a&b%c*(d)
a1b2c3d4-
a
  b
 b  c   defg
thisisaverylongWORDwithnospacesbetweenthewordsandhowwillitbehandled?
thisisaverylongWORDwithnospacesbetweenthewordsandhowwillitbehandled2?
thisisaverylongWORDwithnospacesbetweenthewordsandhowwillitbehandled3?

!"£$%^&*()
Code:
: 3
aid: 3
all: 3
b: 4
c: 3
come: 3
d: 2
defg: 1
for: 3
good: 3
is: 3
men: 3
now: 3
of: 3
party: 4
the: 5
their: 3
thisisaverylongwordwithnospacesbetweenthewordsandhowwillitbehandled: 3
time: 3
to: 5
Code:
['now']
['is', 'the', 'time']
['for', 'all', 'good', 'men']
['to', 'come', 'to', 'the']
['aid', 'of', 'their']
['party', 'now', 'is', 'the', 'time', 'for', 'all', 'good', 'men', 'to']
['come', 'to', 'the', 'aid', 'of', 'their', 'party', 'party', 'their', 'now', 'i
s', 'time', 'the', 'for', 'all', 'good']
['men', 'to', 'come', 'aid', 'of', 'party', 'a', 'b', 'c', 'd']
['a1b2c3d4']
['a']
['b']
['b', 'c', 'defg']
['thisisaverylongwordwithnospacesbetweenthewordsandhowwillitbehandled']
['thisisaverylongwordwithnospacesbetweenthewordsandhowwillitbehandled2']
['thisisaverylongwordwithnospacesbetweenthewordsandhowwillitbehandled3']
[]
[]
Code:
#!/usr/bin/python
import re

fo=open("wordList.txt","r+")
for line in fo:
   line=line.lower()
   #print(line,end='') 
   matches=re.findall(r'\w+',line)
   print(matches)
SunJune is offline   Reply With Quote
Old 03-22-2011, 09:16 AM   PM User | #2
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
Code:
d = {}
f = open('/tmp/in')
for l in f:
  l = l.lower()
  m = re.findall(r'[a-z]+', l)
  for e in m:
    if d.get(e) == None:
      d[e] = 0
    d[e] = d[e] + 1

l = d.keys()
l.sort()
for k in l:
  print '%s : %d' % (k, d[k])
__________________
You never have to change anything you got up in the middle of the night to write. -- Saul Bellow
shyam is offline   Reply With Quote
Users who have thanked shyam for this post:
SunJune (03-23-2011)
Old 03-23-2011, 01:24 PM   PM User | #3
SunJune
New Coder

 
Join Date: Dec 2010
Posts: 19
Thanks: 5
Thanked 0 Times in 0 Posts
SunJune is an unknown quantity at this point
Thanks Shyam for the solution. I am now trying to print the output of this program to a file that is provided by the user at the command line.

Below is the code that I am trying to run, but the problem is that it only generates an empty file. Can you please point out the discrepancy in the code.

Code:
class WriteObject:
  def __init__(self):
   self.content = []
  def write(self, string):
   self.content.append(string)

g= open(sys.argv[2],'w+')
g=WriteObject()
sys.stdout = g
sys.stdout = sys.__stdout__
Thanks
SunJune is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:09 PM.


Advertisement
Log in to turn off these ads.