如何使用 python 以文本模式读取 bz2 压缩文件?

How to read a bz2 compressed file in text mode with python?

我正在尝试使用 python 的 bz2 读取 bzip2 压缩文本文件,但无论模式设置如何,读取的行始终为二进制。

我只是试了一下:

import bz2

with bz2.open("my_file.xml.bz2", mode='r') as fin:
    for line in fin:
        # some processing

如果我对文档的理解正确,"r" 模式应该以文本形式打开文件。但是它是二进制的,有或没有 mode="r".

我做错了什么?

检查the official doc

The mode argument can be any of 'r', 'rb', 'w', 'wb', 'x', 'xb', 'a' or 'ab' for binary mode, or 'rt', 'wt', 'xt', or 'at' for text mode. The default is 'rb'.