简体   繁体   中英

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 ?

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

Support for RAR can be added by an external library .

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM