Replacing characters in a file
<p>I want to replace characters using encoding instructions in a text file.</p>

<p>My text file contains the line: </p>

<pre><code>This is a message
</code></pre>

<p>I want to replace <code>a -&gt; e</code>,<code>e -&gt; a</code>,<code>s -&gt; 3</code></p>

<p>So the line reads: </p>

<pre><code>Thi3 i3 e massega
</code></pre>

<p>I have tried the following code but it only changes one character in the line at one time.</p>

<pre><code>import sys
import codecs

def encode():
  path = "C:\Users\user\Desktop"
  basename = "assgn2part1.txt"
  filename = path + "\\" + basename
  #file = open(filename, "rt")
  f = codecs.open(filename,encoding='utf-8')
  contents = f.read()


  print contents ,"\n"
  newcontents = contents.replace('a','e')
  newcontents = contents.replace('s', '3')

  print newcontents


  f.close()
</code></pre>
