0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

webにあるzip fileをpandasに読み込ませる

Posted at

ローカルにzipをダウンロード -> 解凍 -> pandasに読み込ませるより、
メモリに積まれたままdataframeに落としたかったためのメモ

webにこんな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

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"

# 取得
res = requests.get(url)
# 解凍
z = zipfile.ZipFile(io.BytesIO(res.content))
# 確認
z.namelist() 
# 出力結果
# ['electronic-card-transactions-Feb-2020-csv-tables.csv']

# csvをバイナリで読み込み
with z.open(z.namelist()[0], 'r') as myfile:
    binaryCSV = myfile.read()

# DataFrame
df = pd.read_csv(io.BytesIO(binaryCSV), encoding='utf-8') # 日本語の場合はencoding='sjis'

# 確認
df.head()

image.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?