0

I am using Python 2.7.8 and I have a script which does parsing:

def parse():
    with open('myfile.txt') as f:
.
.
.
Print l

myfile.txt has a UNICODE coding. How can I add a code to this script so that it reads the myfile.txt as ANSI for example?

B A
  • 69
  • 1
  • 8
  • 1
    Why not use `unicode` internally instead? Use `io.open()` and specify the encoding. – Martijn Pieters Sep 08 '14 at 06:52
  • Reading material: http://nedbatchelder.com/text/unipain.html, https://docs.python.org/2/howto/unicode.html, and http://www.joelonsoftware.com/articles/Unicode.html – Martijn Pieters Sep 08 '14 at 06:53
  • 1
    [ANSI](https://en.wikipedia.org/wiki/American_National_Standards_Institute) - is not an encoding. Do you mean [ASCII](https://en.wikipedia.org/wiki/ASCII)? In that case, python opens in ASCII encoding by default. – stalk Sep 08 '14 at 06:59
  • @stalk: The OP is most likely referring to the [Windows code pages](https://en.wikipedia.org/wiki/Windows_code_page#ANSI_code_page), usually [CP-1252](http://en.wikipedia.org/wiki/Windows-1252). – Martijn Pieters Sep 08 '14 at 07:02
  • **Unicode is not an encoding** so the file can't be saved with a Unicode encoding. You might have UTF-8 or UTF-16. – Matthias Sep 08 '14 at 07:39
  • @Matthias, Martijn Pieters: Thanks – B A Sep 08 '14 at 12:00

1 Answers1

2

using io solved my problem:

import io    
def parse()
    with io.open(path,'r',encoding='utf_16') as f:
.....

solved my problem.

B A
  • 69
  • 1
  • 8