Python Create List of Words Per Sentence and Calculate Mean and Place in CSV File
<p>I'm looking to count the number of words per sentence, calculate the mean words per sentence, and put that info into a CSV file. Here's what I have so far. I probably just need to know how to count the number of words before a period. I might be able to figure it out from there.</p>

<pre><code>#Read the data in the text file as a string
with open("PrideAndPrejudice.txt") as pride_file:
    pnp = pride_file.read()

#Change '!' and '?' to '.'
for ch in ['!','?']:
    if ch in pnp:
        pnp = pnp.replace(ch,".")

#Remove period after Dr., Mr., Mrs. (choosing not to include etc. as that often ends a sentence although in can also be in the middle)
pnp = pnp.replace("Dr.","Dr")
pnp = pnp.replace("Mr.","Mr")
pnp = pnp.replace("Mrs.","Mrs")
</code></pre>

<p>You might be interested in the split() function for strings.  It seems like you're editing your text to make sure all sentences end in a period and every period ends a sentence.</p>

<p>Thus,</p>

<pre><code>pnp.split('.')
</code></pre>

<p>is going to give you a list of all sentences.  Once you have that list, for each sentence in the list,</p>

<pre><code>sentence.split() # i.e., split according to whitespace by default
</code></pre>

<p>will give you a list of words in the sentence.</p>

<p>Is that enough of a start?</p>

python csv