title: Python Replace Single Quotes Except Apostrophes
body: <p>I am performing the following operations on lists of words. I read lines in from a Project Gutenberg text file, split each line on spaces, perform general punctuation substitution, and then print each word and punctuation tag on its own line for further processing later. I am unsure how to replace every single quote with a tag  or  excepting all apostrophes. My current method is to use a compiled regex:</p>
  
  <pre><code>apo = re.compile("[A-Za-z]'[A-Za-z]")
  </code></pre>
  
  <p>and perform the following operation:</p>
  
  <pre><code>if "'" in word and !apo.search(word):
      word = word.replace("'","\n&lt;singlequote&gt;")
  </code></pre>
  
  <p>but this  ignores cases where a single quote is used around a word with an apostrophe. It also does not indicate to me whether the single quote is abutting the start of a word of the end of a word.</p>
  
  <p>Example input:</p>
  
  <pre><code>don't
  'George
  ma'am
  end.'
  didn't.'
  'Won't
  </code></pre>
  
  <p>Example output (after processing and printing to file):</p>
  
  <pre><code>don't
  &lt;opensingle&gt;
  George
  ma'am
  end
  &lt;period&gt;
  &lt;closesingle&gt;
  didn't
  &lt;period&gt;
  &lt;closesingle&gt;
  &lt;opensingle&gt;
  Won't
  </code></pre>
  
  <p>I do have a further question in relation to this task: since the distinguishment of <code>&lt;opensingle&gt;</code> vs <code>&lt;closesingle&gt;</code> seems rather difficult, would it be wiser to perform substitutions like </p>
  
  <pre><code>word = word.replace('.','\n&lt;period&gt;')
  word = word.replace(',','\n&lt;comma&gt;')
  </code></pre>
  
  <p><em>after</em> performing the   replacement operation?</p>