To unpack a zip file from url request
To unpack a zip file from url request
I want to get this XML file from a danish website with the URL https://www.foedevarestyrelsen.dk/_layouts/15/sdata/smileystatus.zip, but the data comes in a zip file. This is problematic because I want to get the data sheet directly into Python for a school project, and not to save it local and convert it into csv. So far I have tried following codes inspired from answers to previous questions about this subject....
from zipfile import ZipFile
from io import BytesIO
import requests
import pandas as pd
def get_zip(file_url):
url = requests.get(file_url)
zipfile = ZipFile(BytesIO(url.content))
zip_names = zipfile.namelist()
if len(zip_names) == 1:
SmileyStatus = zip_names.pop()
extracted_file = zipfile.open("SmileyStatus.xls")
return extracted_file
xls = get_zip("https://www.foedevarestyrelsen.dk/_layouts/15/sdata/smileystatus.zip")
data = pd.read_excel(xls)
and I have also tried:
from zipfile import ZipFile
import requests
import pandas as pd
url = 'https://www.foedevarestyrelsen.dk/_layouts/15/sdata/smileystatus.zip'
df = pd.read_csv(url,
compression = "zip")
So from a cursory look,
ZipFile(BytesIO(url.content)
should be what you want. That all looks right. What goes wrong? What errors do you get? Do you get them if you manually download and unzip and just do pd.read_excel('path/to/you/manually/unzipped/SmileyStatus.xls')
(Your second method definitely won't work because a zip file is not a csv, nor is an xls)– Bailey Parker
Aug 24 at 13:51
ZipFile(BytesIO(url.content)
pd.read_excel('path/to/you/manually/unzipped/SmileyStatus.xls')
First of all, it is a xls file, so I gave you the wrong information. Sorry! I get this error message when running my first code: CompDocError: Workbook: size exceeds expected 27286528 bytes; corrupt?
– Helene Østergaard
Aug 24 at 14:20
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
extracted_file = zipfile.open("SmileyStatus.xls") are you sure it is not SmileyStatus.zip?
– E.Serra
Aug 24 at 13:50