'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")
Solution 1:[1]
Know its an old post, but I just had a similar need and found a good solution.
import pandas as pd
import io
import requests
url = 'https://whatever.website/some_file.zip'
r = requests.get(url)
df = pd.read_csv(io.BytesIO(r.content), compression="zip")
print(df.head())
print(f"{len(df)} rows")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Craig9d |
