title: Python write file as UTF-8 instead of ANSI
body: <p>I have run below piece of python code to write a new file replacing a string from the i/p file. It is working fine. But o/p file is getting created with ANSI encoding, whereas input file is utf-8 w/o BOM. Can some one please guide me how to create o/p file with same attribute as i/p file?</p>
  
  <p>Thanks in advance.</p>
  
  <pre><code>import codecs
  
  f1=open(r"D:\Python Programs\New folder\ls.dat","rb")
  f2=open(r"D:\Python Programs\New folder\OUTPUT.DAT","wb")
  
  for line in f1:
      s1=line[0:len(line)]
      s3=s1[72:76].replace(str.encode("HKAG"),str.encode("PNAG"))
      s4=s1[0:72]+s3+s1[76:]
      f2.write(s4)
  
  f1.close()
  f2.close()
  </code></pre>