python - 使用UTF-8打开文件进行读取

标签 python utf-8 readlines

我正在使用下面的代码,但需要打开它以指定 utf-8 进行阅读。请问我该怎么做?

infile = file(logPath)
lines = infile.readlines()

最佳答案

使用codecsopen功能模块:

import codecs

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

默认情况下,codecs.open函数,以rb(读取二进制)模式打开文件:

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.

关于python - 使用UTF-8打开文件进行读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21756347/

相关文章:

python - 使用 readlines 读取前 N 行

python - 如何拆分列表中的元素并从列表中获取日期和时间

python - 使用 Python 的 Subprocess 库避免 SSH 密码提示

python - Groupby 和 count/sum 与 python 中的元组列表?

java - OutOfMemoryError - 来自检测 UTF-8 编码

java - 在java中将字符串从一种编码解释为另一种编码

python - 在 celery 工作人员内部存储数据的常见且明显的方式是什么?

python - 如何阅读此 python 代码?变量 1 = 变量 2 == 变量 3

encoding - 如何定义/声明土耳其语特殊字符(非 ascii)的 utf-8 代码点以将它们用作标准 utf-8 编码?

python - 在 python 中使用 readlines 时忽略 last\n