Question

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()
Was it helpful?

Solution

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top