Cemre Mengü
Cemre Mengü

Reputation: 18754

Open rar files and read csv files using python

I need to write a program which opens some rar files that contain csv files and read them. I know that there are external libraries for this purpose but what is the best way or library to achieve such a task ?

Upvotes: 4

Views: 15601

Answers (2)

simoes
simoes

Reputation: 5036

As stated there is a RAR library for python. That being said you'll still need the unrar program installed on your computer. Using a mac you can install this via homebrew:

brew install unrar

With this installed, your python script might look something like the following:

import rarfile, csv
rar_path = rarfile.RarFile("/path/to/rar_file.rar")
csv_file_name = "rar_file.csv"
rar_file = rarfile.RarFile.open(rar_path, csv_file_name)
csv_reader = csv.reader(rar_file, delimiter=',')

# Should output first line of file (typically CSV header)
print csv_reader.next()

Upvotes: 1

user647772
user647772

Reputation:

Python comes with batteries included. csv is one of these batteries.

Support for RAR can be added by an external library.

Upvotes: 3

Related Questions