Download zip locally-> unzip-> load it into pandas A memo for wanting to drop it in the dataframe while it is still in memory
There was such a url on the web https://www.stats.govt.nz/assets/Uploads/Electronic-card-transactions/Electronic-card-transactions-February-2020/Download-data/electronic-card-transactions-february-2020-csv.zip
import requests
import zipfile
import io
import pandas as pd
# url
url = "https://www.stats.govt.nz/assets/Uploads/Electronic-card-transactions/Electronic-card-transactions-February-2020/Download-data/electronic-card-transactions-february-2020-csv.zip"
#Get
res = requests.get(url)
#Defrost
z = zipfile.ZipFile(io.BytesIO(res.content))
#Verification
z.namelist()
#Output result
# ['electronic-card-transactions-Feb-2020-csv-tables.csv']
#Read csv in binary
with z.open(z.namelist()[0], 'r') as myfile:
binaryCSV = myfile.read()
# DataFrame
df = pd.read_csv(io.BytesIO(binaryCSV), encoding='utf-8') #Encoding for Japanese='sjis'
#Verification
df.head()
Recommended Posts