首页 > 解决方案 > 直接在 Python 中从网站 rar 文件夹中读取 csv 文件

问题描述

我想直接从网站 rar 文件夹中读取 csv 文件。

我有一个 rar 文件夹的链接:

http://.../folder.rar

其中有几个 csv 文件。

如何在 Python 中直接提取每个文件?

我找到了remoteunrar包,并尝试了以下方法(如文档中所述):

import remoteunrar

with remoteunrar('http://.../folder.rar') as rar:
    rar.extract('file.csv')

但它返回一个错误:

TypeError: 'module' object is not callable

(您可以尝试以ecobee_thermostat为例;如果给出相同的错误)

如何在 Python 中直接读取这些文件?

标签: pythoncsvhttprar

解决方案


您可以使用模块和 我的作品zipfile一起使用requests

import requests, zipfile, io
r = requests.get('http://archive.ics.uci.edu/ml/machine-learning-databases/00442/Ecobee_Thermostat/mirai_attacks.rar?')
z = zipfile.ZipFile(io.BytesIO(r.content))
z.extractall("/path/location")

推荐阅读