python-3.x - 读取 zip 文件的内容而不解压

标签 python-3.x zip

我想要实现的目标的示例:

我的文本文件(test1.txt)包含以下两行:

John scored 80 in english

tim scored 75 in english

我已将此文件压缩为 test1.zip,我尝试使用以下代码读取内容:

f = 'test1.zip'
z = zipfile.ZipFile(f, "r")
zinfo = z.namelist()
for name in zinfo:
    with z.open(name) as f1:
        fi1 = f1.readlines()
for line in fi1:
print(line)

但是我得到的结果是

b'John scored 80 in english\r\n'

b'tim scored 75 in english\r\n'

如何读取此 zip 文件的内容,该文件应提供与原始文件内容相同的输出,即:

John scored 80 in english

tim scored 75 in english

最佳答案

您实际上正在阅读文件中的具体内容。

/r/n 字符是 Windows 中的换行符。问题 Difference between \n and \r?更详细一点,但归根结底是 Windows 使用/r/n 作为换行符。

您看到的 b' 字符与 python 及其解析文件的方式有关。问题What does the 'b' character do in front of a string literal?很好地回答了为什么会发生这种情况,但引用的文档是:

Bytes literals are always prefixed with 'b' or 'B'; they produce an instance of the bytes type instead of the str type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes.

编辑:我实际上找到了一个非常相似的答案,您可以从中获取无需额外字符的阅读:py3k: How do you read a file inside a zip file as text, not bytes? 。基本想法是你可以使用这个:

items_file  = io.TextIOWrapper(items_file, encoding='your-encoding', newline='')

关于python-3.x - 读取 zip 文件的内容而不解压,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37506232/

相关文章:

zip - 压缩(放气、gzipped)内容结构的可视化

python-2.7 - 无法从 Explorer [2013] 通过 IDLE 运行 Python - IDLE 的子进程未建立连接

python - Thead.join() 仅在所有线程完成后才帮助打印字符串

Ruby:读取临时 zip 文件

python - 将多列合并为矩阵python

c# - 在 Azure 本地存储下压缩文件时出错 "The directory Name is Invalid"

python - python 中的错误处理

python - raw_input ("") 已从 python 3.2 中删除

python - 如何在树莓派中更新 python

python-3.x - 在pyspark中添加UUID的有效方法