In [10]:
from collections import Counter
#import re

word_dict = Counter()
#  with will automaticlly close your file
with open('text.txt', 'r') as file:
    for line in file:
        print "line is: ", line
        print "line.split() is: \n", line.split()
        print " "
        word_dict.update(line.split())
#word_dict


line is:  This module implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple.

line.split() is: 
['This', 'module', 'implements', 'specialized', 'container', 'datatypes', 'providing', 'alternatives', 'to', 'Python\xe2\x80\x99s', 'general', 'purpose', 'built-in', 'containers,', 'dict,', 'list,', 'set,', 'and', 'tuple.']
 
line is:  

line.split() is: 
[]
 
line is:  In addition to the concrete container classes, the collections module provides abstract base classes that can be used to test whether a class provides a particular interface, for example, whether it is hashable or a mapping.

line.split() is: 
['In', 'addition', 'to', 'the', 'concrete', 'container', 'classes,', 'the', 'collections', 'module', 'provides', 'abstract', 'base', 'classes', 'that', 'can', 'be', 'used', 'to', 'test', 'whether', 'a', 'class', 'provides', 'a', 'particular', 'interface,', 'for', 'example,', 'whether', 'it', 'is', 'hashable', 'or', 'a', 'mapping.']
 
line is:  

line.split() is: 
[]
 
line is:  In some ways, the United States has made some progress. Fires no longer destroy 18,000 buildings as they did in the Great Chicago Fire of 1871, or kill half a town of 2,400 people, as they did the same night in Peshtigo, Wisconsin. Other than the Beverly Hill Supper Club fire in Kentucky in 1977, it has been four decades since more than 100 Americans died in a fire.

line.split() is: 
['In', 'some', 'ways,', 'the', 'United', 'States', 'has', 'made', 'some', 'progress.', 'Fires', 'no', 'longer', 'destroy', '18,000', 'buildings', 'as', 'they', 'did', 'in', 'the', 'Great', 'Chicago', 'Fire', 'of', '1871,', 'or', 'kill', 'half', 'a', 'town', 'of', '2,400', 'people,', 'as', 'they', 'did', 'the', 'same', 'night', 'in', 'Peshtigo,', 'Wisconsin.', 'Other', 'than', 'the', 'Beverly', 'Hill', 'Supper', 'Club', 'fire', 'in', 'Kentucky', 'in', '1977,', 'it', 'has', 'been', 'four', 'decades', 'since', 'more', 'than', '100', 'Americans', 'died', 'in', 'a', 'fire.']
 
line is:  

line.split() is: 
[]
 
line is:  But even with such successes, the United States still has one of the worst fire death rates in the world. Safety experts say the problem is neither money nor technology, but the indifference of a country that just will not take fires seriously enough.

line.split() is: 
['But', 'even', 'with', 'such', 'successes,', 'the', 'United', 'States', 'still', 'has', 'one', 'of', 'the', 'worst', 'fire', 'death', 'rates', 'in', 'the', 'world.', 'Safety', 'experts', 'say', 'the', 'problem', 'is', 'neither', 'money', 'nor', 'technology,', 'but', 'the', 'indifference', 'of', 'a', 'country', 'that', 'just', 'will', 'not', 'take', 'fires', 'seriously', 'enough.']
 

In [20]:
import string
with open('text.txt', 'r') as file:
    for line in file:
        print "line is: \n", line.rstrip()
        print "line.translate(None, string.punctuation) is: \n", line.rstrip().translate(None, string.punctuation).split()
        #print "line.split() is: \n", line.split()


line is: 
This module implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple.
line.translate(None, string.punctuation) is: 
['This', 'module', 'implements', 'specialized', 'container', 'datatypes', 'providing', 'alternatives', 'to', 'Python\xe2\x80\x99s', 'general', 'purpose', 'builtin', 'containers', 'dict', 'list', 'set', 'and', 'tuple']
line is: 

line.translate(None, string.punctuation) is: 
[]
line is: 
In addition to the concrete container classes, the collections module provides abstract base classes that can be used to test whether a class provides a particular interface, for example, whether it is hashable or a mapping.
line.translate(None, string.punctuation) is: 
['In', 'addition', 'to', 'the', 'concrete', 'container', 'classes', 'the', 'collections', 'module', 'provides', 'abstract', 'base', 'classes', 'that', 'can', 'be', 'used', 'to', 'test', 'whether', 'a', 'class', 'provides', 'a', 'particular', 'interface', 'for', 'example', 'whether', 'it', 'is', 'hashable', 'or', 'a', 'mapping']
line is: 

line.translate(None, string.punctuation) is: 
[]
line is: 
In some ways, the United States has made some progress. Fires no longer destroy 18,000 buildings as they did in the Great Chicago Fire of 1871, or kill half a town of 2,400 people, as they did the same night in Peshtigo, Wisconsin. Other than the Beverly Hill Supper Club fire in Kentucky in 1977, it has been four decades since more than 100 Americans died in a fire.
line.translate(None, string.punctuation) is: 
['In', 'some', 'ways', 'the', 'United', 'States', 'has', 'made', 'some', 'progress', 'Fires', 'no', 'longer', 'destroy', '18000', 'buildings', 'as', 'they', 'did', 'in', 'the', 'Great', 'Chicago', 'Fire', 'of', '1871', 'or', 'kill', 'half', 'a', 'town', 'of', '2400', 'people', 'as', 'they', 'did', 'the', 'same', 'night', 'in', 'Peshtigo', 'Wisconsin', 'Other', 'than', 'the', 'Beverly', 'Hill', 'Supper', 'Club', 'fire', 'in', 'Kentucky', 'in', '1977', 'it', 'has', 'been', 'four', 'decades', 'since', 'more', 'than', '100', 'Americans', 'died', 'in', 'a', 'fire']
line is: 

line.translate(None, string.punctuation) is: 
[]
line is: 
But even with such successes, the United States still has one of the worst fire death rates in the world. Safety experts say the problem is neither money nor technology, but the indifference of a country that just will not take fires seriously enough.
line.translate(None, string.punctuation) is: 
['But', 'even', 'with', 'such', 'successes', 'the', 'United', 'States', 'still', 'has', 'one', 'of', 'the', 'worst', 'fire', 'death', 'rates', 'in', 'the', 'world', 'Safety', 'experts', 'say', 'the', 'problem', 'is', 'neither', 'money', 'nor', 'technology', 'but', 'the', 'indifference', 'of', 'a', 'country', 'that', 'just', 'will', 'not', 'take', 'fires', 'seriously', 'enough']

In [40]:
import string

with open('text.txt', 'r') as f:
    #i = 0
    for line in f.readlines():
        if line != "\n":
            #print i
            print line.strip().translate(None, string.punctuation).lower()


this module implements specialized container datatypes providing alternatives to python’s general purpose builtin containers dict list set and tuple
in addition to the concrete container classes the collections module provides abstract base classes that can be used to test whether a class provides a particular interface for example whether it is hashable or a mapping
in some ways the united states has made some progress fires no longer destroy 18000 buildings as they did in the great chicago fire of 1871 or kill half a town of 2400 people as they did the same night in peshtigo wisconsin other than the beverly hill supper club fire in kentucky in 1977 it has been four decades since more than 100 americans died in a fire
but even with such successes the united states still has one of the worst fire death rates in the world safety experts say the problem is neither money nor technology but the indifference of a country that just will not take fires seriously enough

In [14]:
%run play4_re.py


the number of words in the file is: 172

In [27]:
i = 0
with open('text.txt', 'r') as f:
    for line in f.read():
        #print line.strip('\n')
        print line
        i += 1
        print(line.strip('\n'))


<built-in method strip of str object at 0x103e40cb0>
<built-in method strip of str object at 0x10069c288>
<built-in method strip of str object at 0x10401f240>
<built-in method strip of str object at 0x10069c288>
<built-in method strip of str object at 0x104028cf0>
<built-in method strip of str object at 0x10069c288>
<built-in method strip of str object at 0x103fe63a8>
<built-in method strip of str object at 0x10069c288>

In [28]:
import fileinput
for line in fileinput.FileInput('text.txt',inplace=1):
    if line.rstrip():
        print line

In [39]:
%run play4_counter.py


Counter({'the': 11, 'in': 6, 'a': 6, 'of': 4, 'to': 3, 'has': 3, 'fire': 3, 'United': 2, 'they': 2, 'did': 2, 'some': 2, 'module': 2, 'container': 2, 'provides': 2, 'or': 2, 'is': 2, 'that': 2, 'than': 2, 'whether': 2, 'classes': 2, 'it': 2, 'as': 2, 'States': 2, 'In': 2, 'just': 1, 'money': 1, 'alternatives': 1, 'four': 1, 'kill': 1, 'Hill': 1, 'still': 1, 'death': 1, 'such': 1, 'dict': 1, '2400': 1, 'decades': 1, 'worst': 1, 'half': 1, 'not': 1, 'world': 1, '18000': 1, 'nor': 1, 'list': 1, 'night': 1, 'set': 1, 'people': 1, 'buildings': 1, 'fires': 1, 'even': 1, 'for': 1, 'ways': 1, 'since': 1, 'seriously': 1, 'progress': 1, 'neither': 1, 'be': 1, 'This': 1, 'base': 1, 'interface': 1, '100': 1, 'hashable': 1, 'successes': 1, 'concrete': 1, 'Americans': 1, 'enough': 1, 'one': 1, 'Safety': 1, 'But': 1, '1871': 1, 'addition': 1, 'Python\xe2\x80\x99s': 1, 'been': 1, 'rates': 1, 'Supper': 1, 'collections': 1, 'containers': 1, 'more': 1, 'implements': 1, 'but': 1, 'particular': 1, 'Beverly': 1, 'with': 1, 'town': 1, 'made': 1, 'indifference': 1, 'say': 1, 'will': 1, 'Fires': 1, 'can': 1, 'country': 1, 'problem': 1, 'providing': 1, 'specialized': 1, 'example': 1, 'and': 1, 'Chicago': 1, 'abstract': 1, 'general': 1, 'Other': 1, 'experts': 1, 'technology': 1, 'no': 1, 'Club': 1, 'tuple': 1, 'same': 1, 'builtin': 1, 'take': 1, 'test': 1, 'destroy': 1, 'Great': 1, 'used': 1, 'mapping': 1, 'Kentucky': 1, 'purpose': 1, 'Wisconsin': 1, 'datatypes': 1, 'class': 1, 'died': 1, 'Peshtigo': 1, 'longer': 1, '1977': 1, 'Fire': 1})

In [ ]:


In [ ]: