Split function splitting words with punctuations. Want to prevent. After splitting how to Alphabetize?
<p>I have two issues. Here goes the code:    </p>

<pre><code>Read =open("C:\Users\Moondra\Desktop/test1.txt",'r')
text =Read.read()
words =text.split()
print(words)
print(words.sort())

##counts=dict()
##for word in words:
##    counts[word] = counts.get(word,0)+1
##
##
##print counts    
</code></pre>

<p>And the text that I am trying to read:</p>

<p><strong>test1.txt</strong></p>

<p>Hello Hello Hello.</p>

<p>How is everything. What is going on?
Where are you? Hello!!</p>

<p>Hope to see you soon.</p>

<p>When are you coming by?</p>

<p>What should I make for dinner?</p>

<p>The end!</p>

<p><strong>End of text from txt file</strong></p>

<p>My two questions are the following:</p>

<ol>
<li><p>I'm trying to implement a count-each-word code where I count the number of times each word appears in a document. 
However when I split the words using the above code, the word "Hello" will appear as "Hello!," or even "Hello." separately.  How can I avoid this? </p></li>
<li><p>Next, I tried to sort the elements of the list, alphabetically, but all I get in return after running the <code>sort()</code> method is <code>none</code> which is really confusing me. </p></li>
</ol>

<p>Thanks!</p>

<p>This code should work for what you described:</p>

<pre><code>import re

with open("C:\Users\Moondra\Desktop/test1.txt", 'r') as file:
    file = file.read()

words_list = re.findall(r"[\w]+", file)
words_list = sorted(words_list, key=str.lower)

patterns = ["Hello"]
counter = 0


for word in words_list:
    for pattern in patterns:
        if word == pattern:
            counter+=1

print("The word Hello occurred {0} times".format(counter)) # prints the number of times 'Hello' was found
print(words_list) # prints your list alphabetically
</code></pre>

<p>There are a few things you should note however:</p>

<ul>
<li>I used the <code>re</code> module instead of sort. This is because using the <a href="https://docs.python.org/2/library/re.html" rel="nofollow">regular expression</a> engine in the re module would be much less complex than trying to split the strings using the <code>split()</code> function.</li>
<li>I renamed some of your variables to follow the <a href="https://www.python.org/dev/peps/pep-0008/" rel="nofollow">PEP8</a> guide and naming convention for Python. Feel free to rename to your liking.</li>
<li>the reason that <strong><code>sort()</code></strong> returned a list, is because the <code>sort()</code> attribute of a list, does not return a new list, but changes the old one. That is, the <code>sort()</code> attribute of a list sorts in place. The <code>sort()</code> you were using returns the data type <code>None</code>. You need to use the builtin Python function  <strong><code>sorted()</code></strong> instead. The <code>sorted()</code> function returns the data type <code>list</code>.</li>
</ul>

python text split