LRD
LRD

Reputation: 361

Read csv files from website rar folder directly in Python

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

Answers (2)

zzhapar
zzhapar

Reputation: 135

I solved this problem like below:

  1. created folder for extract files
  2. extracted all to 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')
  1. got needed files
  2. deleted all files with rar

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

Shabari nath k
Shabari nath k

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

Related Questions