1

I am unable to open a zipped file from a web.

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')

And when I try to print some data, I got a bunch of numbers. There are other txt files in that zip, and when I replace the file name in content, I got the same output. Although I read py3k: How do you read a file inside a zip file as text, not bytes?, I don't know how to fix it.

Sliwa
  • 61
  • 8
  • Possible duplicate of [py3k: How do you read a file inside a zip file as text, not bytes?](https://stackoverflow.com/questions/5627954/py3k-how-do-you-read-a-file-inside-a-zip-file-as-text-not-bytes) – ololobus Mar 21 '19 at 16:43
  • According to the docs https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile.read read returns plain bytes, not text. You could use TextIOWrapper – ololobus Mar 21 '19 at 16:45

1 Answers1

1

The problem was with method of 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)

This code fixed my problem and I was able to look up data in the zipfile. Read has been replaced by open.

Sliwa
  • 61
  • 8