[英]Unable to read zipped file from url
我無法從網絡打開壓縮文件。
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? ,不知道怎么解決。
問題出在 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 中查找數據。 讀取已被打開取代。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.