Reputation: 361
I would like to read csv files directly from a website rar folder.
I have a link to a rar folder:
http://.../folder.rar
in which there're several csv files.
How can I extract each single file directly in Python?
I found the remoteunrar package, and tried the following (as explained in the documentation):
import remoteunrar
with remoteunrar('http://.../folder.rar') as rar:
rar.extract('file.csv')
but it returns an error:
TypeError: 'module' object is not callable
(You can try with ecobee_thermostat as an example; if gives the same error)
How can I read these files directly in Python?
Upvotes: 1
Views: 1240
Reputation: 135
I solved this problem like below:
FolderForExtract
(empty folder)from pyunpack import Archive
from zipfile import ZipFile
import rarfile
def extract_rar(file):
rar = rarfile.RarFile(file)
Archive(file).extractall('FolderForExtract')
def extract_zip(file):
zip = ZipFile(file)
zip.extractall('FolderForExtract')
I couldn't find other ways
Also I tried read rar by link, but couldn't extract. Maybe this code can help find another way to unpack rar using link:
from io import BytesIO
from pyunpack import Archive
import urllib.request
import rarfile
import patoolib
resp = urllib.request.urlopen("url")
r = rarfile.RarFile(BytesIO(resp.read()))
print(r.namelist())
# patoolib.extract_archive(resp.read(), '.')
# Archive(resp.read()).extractall('.')
Upvotes: 1
Reputation: 890
you can use zipfile
module along with requests
this works form me
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")
Upvotes: 2