简体   繁体   中英

Using UTF-8 to open file for reading

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()

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. 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM