【问题标题】:Unable to read zipped file from url无法从 url 读取压缩文件
【发布时间】:2019-08-12 14:12:53
【问题描述】:

我无法从网络打开压缩文件。

from urllib.request import urlopen
from io import BytesIO
from zipfile import ZipFile
url = "http://..../craft.zip"
file = urlopen(url).read()
file = BytesIO(file)
document = ZipFile(file)
content = document.read('MASTER.txt')

当我尝试打印一些数据时,我得到了一堆数字。该 zip 中还有其他 txt 文件,当我替换内容中的文件名时,我得到了相同的输出。虽然我读了py3k: How do you read a file inside a zip file as text, not bytes?,但我不知道如何解决它。

【问题讨论】:

标签: python python-3.x urllib zipfile


【解决方案1】:

问题出在 zipfile 的方法上:

from urllib.request import urlopen
from io import BytesIO
from zipfile import ZipFile

url = "http://....craft.zip"
file = urlopen(url).read()
file = BytesIO(file)
document = ZipFile(file)
content = document.open('MASTER.txt', "r")
for line in content:
        print(line)

这段代码解决了我的问题,我可以在 zipfile 中查找数据。阅读已被打开取代。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    • 2013-11-16
    相关资源
    最近更新 更多