Get pattern ae|ea|s3 to replace characters in string in python
<p>I want to give encoding instructions  ‘ae|ea|s3’, where a is replaced by e, e is replace by a and s is replaced by 3 for the same string. But I am having problem to write a program to insert pattern chr replaced by chr1|chr2 replaced by chr3|chr4 replaced by chr5. for the above pattern chr, chr2 and chr4 are characters from the string i want to replace by chr1, chr3,chr5 and "|" represents "and". I have searched google but couldnt find any hints regarding the above problem.</p>

<pre><code>def encode(filename):

  path = "C:\Users\user\Desktop"
  basename = "assgn2part1.txt"
  filename = path + "\\" + basename
  #file = open(filename, "rt")
  f = open(filename)
  contents = f.read()
  print contents ,"\n"
  chars = ["a","b","e","3","d","r","g","j","h","i"]

  newcontents = contents.translate(string.maketrans("aes","ea3"))

  print newcontents 
</code></pre>

<p>The answer I get is:</p>

<pre><code>  This is a message 

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

<p>This does give the result, but I want to insert the pattern in the definition itself</p>

<pre><code>  encode("ae|ea|s3","C:\Users\user\Desktop\\assgn2part1.txt")
</code></pre>

<p>to give me the result and I should be able to substitute it with another type of pattern like</p>

<pre><code>  encode("hc|m3|te","C:\Users\user\Desktop\\assgn2part1.txt")
</code></pre>

<p>I have tried very hard but not able to get solution for this. Please help me</p>
