How to count the amount of times a substring is in a csv file using python -


as title implies, trying figure out how code program allow me count how many times substring appears in csv file. example have csv file states how many time person called unknown number , want write file amount of times each person called number unknown.

this input csv file like:

john smith called or messaged unknown party(xxx-xxx-xxxx) on 11/9/2013 @ 16:44 1 second(s).   john smith called or messaged unknown party(xxx-xxx-xxxx) on 11/12/2013 @ 8:18 1 second(s).   john smith called or messaged unknown party(xxx-xxx-xxxx) on 11/21/2013 @ 16:17 1 second(s).   john smith called or messaged unknown party(xxx-xxx-xxxx) on 11/21/2013 @ 13:51 1 second(s).   john smith called or messaged unknown party(yyy-yyy-yyyy) on 11/1/2013 @ 16:26 1 second(s).   john smith called or messaged unknown party(yyy-yyy-yyyy) on 11/1/2013 @ 16:45 21 second(s).   jane smith called or messaged unknown party(zzz-zzz-zzzz) on 11/21/2013 @ 10:41 2 second(s).   jane smith called or messaged unknown party(zzz-zzz-zzzz) on 11/21/2013 @ 15:52 4 second(s).   

an example of output file want follows:

john smith called or messaged unknown party 6 time(s).       jane smith called or messaged unknown party 2 time(s).    

any , appreciated. if have not explained problem, please let me know. thanks!

assuming file of interest called 'filename.txt', can use:

from collections import defaultdict  counts = defaultdict(int) open('filename.txt', 'r') f:   line in f:     info = line.split('(')[0]     counts[info] += 1  c in counts:   print '%s %d time(s).' % (c, counts[c]) 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -