【问题标题】:Using Pandas, how to read a csv file inside a zip file which you fetch using an url[Python]使用 Pandas,如何读取使用 url [Python] 获取的 zip 文件中的 csv 文件
【发布时间】:2020-05-19 23:39:04
【问题描述】:

这个网址 https://ihmecovid19storage.blob.core.windows.net/latest/ihme-covid19.zip

包含 2 个 csv 文件和 1 个每天更新的 pdf,其中包含 Covid-19 数据。

我希望能够将 Summary_stats_all_locs.csv 加载为 Pandas DataFrame。

通常,如果有一个指向 csv 的 url,我可以使用 df = pd.read_csv(url),但由于 csv 在 zip 中,我不能在这里这样做。

我该怎么做?

谢谢

【问题讨论】:

标签: python pandas zip


【解决方案1】:

您需要先获取文件,然后使用ZipFile 模块加载它。 Pandas 实际上可以从 zip 中读取 csv,但这里的问题是有多个,所以我们需要这样做并指定文件名。

import requests
import pandas as pd
from zipfile import ZipFile
from io import BytesIO

r = requests.get("https://ihmecovid19storage.blob.core.windows.net/latest/ihme-covid19.zip")
files = ZipFile(BytesIO(r.content))
pd.read_csv(files.open("2020_05_16/Summary_stats_all_locs.csv"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-23
    • 2016-09-30
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 2010-12-08
    • 2019-08-09
    相关资源
    最近更新 更多