【问题标题】:Requesting a .gz file then opening as a dataframe请求 .gz 文件,然后作为数据框打开
【发布时间】:2022-01-06 21:31:29
【问题描述】:

这里是非常新的 python 用户。我有一个压缩数据集 (csv.gz) 的链接,我试图在没有先下载到我的桌​​面、解压缩并上传到 github 以使用 pd.read_csv 的情况下进入 python。该链接的形式是我认为我应该使用请求来请求它?但我得到一个 403 状态码。我不知道是因为我请求错误,还是 .gz 是问题?

这就是我尝试导入它的方式。

import requests
query = {'AWSAccessKeyId':'theaccesskey', 'Signature':'thesignature','Expires':'12345'}
url = 'https://s3.amazonaws.com/research.insideairbnb.com/data/united-states/or/portland/2015-12-02/data/listings.csv.gz'

response = requests.get(url, params=query) 

但即使假设我可以弄清楚如何获得 200 状态码,我如何从 .gz 转到我可以使用 pandas 的数据框?

【问题讨论】:

  • 您可以使用io.BytesIOpandas.read_csv。有趣的是,您的网址会生成 JPEG 图像而不是 .gz 文件。是正确的 url / 参数吗?
  • 我输入了虚拟参数

标签: python api


【解决方案1】:

您可以将io.BytesIOpandas.read_csv 一起使用:

import requests
import pandas as pd
from io import BytesIO

url = 'https://github.com/apache-superset/examples-data/blob/master/san_francisco.csv.gz'

# This is required to access raw binary files on Github
# i.e. it appends the following to the URL: `?raw=true`.
query = {'raw': 'true'} 

# Use the requests module to parse the URL with the provided parameters.
response = requests.get(url, params=query)

# Create a file pointer initialized to the content of the response
# using `BytesIO`. This is a psuedo-file, which can now be read
# using `pandas.read_csv`. Since `response.content` is binary data
# i.e. bytes, we use `BytesIO`. If the response was text, we would
# have used `StringIO`.
fp = BytesIO(response.content)

# Finally, parse the content into a DataFrame 
# (populate other parameters as needed).
df = pd.read_csv(fp, compression='gzip')
print(df)

这应该将csv.gz 文件的内容作为DataFrame 返回。在此示例中使用 URL 会产生以下输出:

               LON        LAT NUMBER            STREET UNIT  CITY  DISTRICT  REGION  POSTCODE  ID
0      -122.391267  37.769093   1550       04th Street  NaN   NaN       NaN     NaN     94158 NaN
1      -122.390850  37.769426   1505       04th Street  NaN   NaN       NaN     NaN     94158 NaN
2      -122.428577  37.780627   1160   Buchanan Street  NaN   NaN       NaN     NaN     94115 NaN
3      -122.428534  37.780385   1142   Buchanan Street  NaN   NaN       NaN     NaN     94115 NaN
4      -122.428525  37.780317   1140   Buchanan Street  NaN   NaN       NaN     NaN     94115 NaN
...            ...        ...    ...               ...  ...   ...       ...     ...       ...  ..
261547 -122.418380  37.808349    360  Jefferson Street  NaN   NaN       NaN     NaN     94133 NaN
261548 -122.418380  37.808349    350  Jefferson Street  NaN   NaN       NaN     NaN     94133 NaN
261549 -122.417829  37.807479    333  Jefferson Street  NaN   NaN       NaN     NaN     94133 NaN
261550 -122.418916  37.809044   1965      Al Scoma Way  NaN   NaN       NaN     NaN     94133 NaN
261551 -122.444322  37.749124    350    Glenview Drive  NaN   NaN       NaN     NaN     94131 NaN

我使用了我在网上找到的示例 csv.gz 文件,因为 URL 和参数会生成 JPEG 图像,这有点令人费解。无论如何,根据您的情况调整这段代码,它应该会产生所需的结果。

【讨论】:

  • @MargotBlack 没有问题!我认为需要一些特定的标头,因为您共享的链接可以从浏览器、curl 和 wget 工作 - 该文件可以单独下载。但是,使用 requests 库时,它会产生 403。我粗略地搜索了将常见标头(例如 headers={'User-agent': 'Mozilla/5.0'})应用于请求,但我尝试应用它们并没有产生任何结果。此外,您可能希望删除或使您的访问密钥 ID 和相关凭据无效,因为这可能会被滥用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-09
  • 2022-10-02
  • 1970-01-01
  • 1970-01-01
  • 2014-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多