2

I am using the below code but need to open it for reading with utf-8 specified. How would I do that please?

infile = file(logPath)
lines = infile.readlines()
speedyrazor
  • 3,127
  • 7
  • 33
  • 51

1 Answers1

3

Use open function of codecs module:

import codecs

with codecs.open(logPath, encoding='utf8') as infile:
    lines = infile.readlines()

By default the codecs.open function, open the file in rb (read binary) mode:

def open(filename, mode='rb', encoding=None, errors='strict', buffering=1):

    ...
    Files are always opened in binary mode, even if no binary mode
    was specified. This is done to avoid data loss due to encodings
    using 8-bit values. The default file mode is 'rb' meaning to
    open the file in binary read mode.
Omid Raha
  • 9,862
  • 1
  • 60
  • 64
  • Can I use this infile = open(logPath, encoding='utf8') – speedyrazor Feb 13 '14 at 14:15
  • This gives me error: infile = open(logPath, encoding='utf8') TypeError: 'encoding' is an invalid keyword argument for this function – speedyrazor Feb 13 '14 at 14:15
  • Now using infile = codecs.open(logPath, encoding='utf8') Anyone see any problems with this? – speedyrazor Feb 13 '14 at 14:20
  • I don't see problems? Do you have problems? – Matthias Feb 13 '14 at 14:28
  • No, in `Python 2`, you can't use `open(logPath, encoding='utf8')`, because it's a built-in [open](http://docs.python.org/2/library/functions.html#open) function, and is different from `codecs.open` function. But in `Python 3` you can use the built-in [open](http://docs.python.org/3/library/functions.html#open) function for that, however it is still different from `codecs.open` function. – Omid Raha Feb 13 '14 at 15:50