PayPay Member
Posts : 3 Join date : 2011-05-11
| Subject: count the item in a list without any numbers and quotations Wed May 11, 2011 11:36 pm | |
| 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)
|
|
MoonLight Member
Posts : 5 Join date : 2011-05-11
| Subject: Re: count the item in a list without any numbers and quotations Wed May 11, 2011 11:37 pm | |
| - 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])
|
|
PayPay Member
Posts : 3 Join date : 2011-05-11
| Subject: Re: count the item in a list without any numbers and quotations Wed May 11, 2011 11:38 pm | |
| Thanks moonlight 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 |
|
Sponsored content
| Subject: Re: count the item in a list without any numbers and quotations | |
| |
|